2

I'm new with Python and I need to build a Python library that connect to our API service sending some json data, everything works fine but one thing, I need to send some dict of dicts doing a POST request:

    def create_project(self, project):
        print project
        params = simplejson.dumps(project)
        print params
        req = requests.post(self.url+'/projects/addSpeedy.json',
            data=params, 
            auth=HTTPBasicAuth(self.api_id, self.api_key),verify=False)
        data = simplejson.loads(req.text)
        return data

the project param I'm passing to that function contains the following structure:

    script = {
        'part001': 'HI',
        'part002': 'WORLD'
    }
    project = {
        'title': 'Project posted from Python Carrot',
        'script': script,
        'remarks': "I want the voice be similar to Bugs Bunny.",
        'test': '1'
    }

However when doing the request the API tells me the 'title' field which is required is missing, however when printing the data in the function everything seems fine, the dict json encode was something I saw in the requests site for this cases: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

I've tried other ways with mixed results it doesnt work the way it should, also the problem its not the API since we have libraries in other languages and it works fine.

4

1 回答 1

0

Hard to guess, what's going wrong without further details about your http client and the server API. My first try debugging this would be to grab wireshark and check, if your generated requests are identical to the ones generated with the implementation of your other clients with the following questions to answer:

  • Are some headers different?
  • Content-Length and Content-Type specified correctly?
  • Did the authentication succeed or is the response with the missing title used quite generically?
于 2012-09-28T03:57:07.843 回答