Frage

I'm trying to submit a simple post to an API, and it's telling me I'm missing fields. The same data works when using a Chrome plugin to post.

Can you see anything wrong with the way I'm doing this?

def post(self, url, **kwargs):

        results = requests.post(
            self.host + url,
            params=json.dumps(kwargs),
            headers=self.headers)
        print "Original params: ", kwargs
        print "Ok?: ", results.ok
        print "Reason: ", results.reason
        print "JSON: ", results.json()

OUT:

Original params:  {'name': 'The Dants', 'id': 'Dant'}
Ok?:  False
Reason:  BAD REQUEST
JSON:  {u'id': [u'This field is required.'], u'name': [u'This field is required.']}
War es hilfreich?

Lösung

params puts the content in a query string. You want to put your content in the body of the POST request. It is possible to have both params and a body in a POST request, but that's not necessary here.

Change params= to data= to put your json in the body.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top