user2954587
user2954587

Reputation: 4861

django nested body request not being set

I'm trying to make a request to the box api using python and django. I'm getting a 400 Entity body should be a correctly nested resource attribute name\\/value pair error.

My requests looks like:

requests.options(headers.kwargs['url'], headers=headers.headers, 
                 data={'parent': {'id': 'xxxx'}, 'name': 'name.pdf'})

When I inspect the 400 request.body it contains 'parent=id&name=name.pdf' which leads me to believe I'm not setting the body properly

A curl works with the body

-d '{"name": "name.pdf", "parent": {"id": "xxxxx"}}'

Upvotes: 0

Views: 78

Answers (1)

falsetru
falsetru

Reputation: 369394

Explicitly encode the dictionary to prevent form-encoding. Otherwise, it will be form-encoded as the way similar to urllib.urlencode (or urllib.parse.urlencode in Python 3.x).

import json

...

requests.options(
    headers.kwargs['url'], headers=headers.headers, 
    data=json.dumps({'parent': {'id': 'xxxx'}, 'name': 'name.pdf'}))

In other word, instead of passing a dictionary, pass a string.


According to More complicated POST requests - Request documentation:

... There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.

Upvotes: 1

Related Questions