Icallitvera
Icallitvera

Reputation: 176

Invalid JSON with box-api

 def get_item_url(itemid, itemtype):
if itemtype=="folder" or itemtype=="FOLDER":
    url = "https://api.box.com/2.0/folders/"+itemid
    headers = {'Authorization' : 'BoxAuth api_key='+apikey+'&auth_token='+auth_token,}
    payload = {'shared_link': {'access': 'Open'}}
    r = requests.request("PUT", url, None, json.dumps(payload), headers)
    print r.content
    return r.content
elif itemtype=="file" or itemtype=="FILE":
    url = "https://api.box.com/2.0/files/"+itemid
    headers = {'Authorization' : 'BoxAuth api_key='+apikey+'&auth_token='+auth_token,}
    payload = {'shared_link': {'access': 'Open'}}
    r = requests.request("PUT", url, None, json.dumps(payload), headers)
    print r.content
    return r.content

This is the code that I have to get a item url, either a file or a folder depending on the string passed to the method. But this does not work the Box API always returns a "Invalid JSON" error. Could anyone help me out, I have tried everythin! I tried using double quote, single quotes, putting just 'payload' instead of running it through the json.dumps. I have tried adding it as another header, EVERYTHING but I just cannot get it to work!

I have also tried doing the same requests to httpbin.org/put and this is what I get in return:

{
"origin": "10.217.61.17",
"files": {},
"form": {},
"url": "http://httpbin.org/put",
"args": {},
"headers": {
"Content-Length": "",
"Accept-Encoding": "identity, deflate, compress, gzip",
"Accept": "*/*",
"Host": "httpbin.org",
"Content-Type": "",
"Authorization": "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>"
},
"json": null,
"data": ""
}

Upvotes: 0

Views: 1150

Answers (3)

Ben Zittlau
Ben Zittlau

Reputation: 2485

Well, with the httpbin response it's definitely an issue with the request itself, not the Box API.

Try formatting the request like the example here: http://docs.python-requests.org/en/latest/user/quickstart/#custom-headers

>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}

>>> r = requests.post(url, data=json.dumps(payload), headers=headers)

Upvotes: 1

seanrose
seanrose

Reputation: 8685

A few things

You should explicitly set the arguments for each Requests HTTP method and also use the built in functions for making HTTP requests instead of directly using the request object, i.e. instead of

r = requests.request("PUT", url…)

You should instead do

r = requests.put(url…)

If you do that (and explicitly set the arguments), your new logic should look like:

url = "https://api.box.com/2.0/files/"+itemid
headers = {'Authorization' : 'BoxAuth api_key='+apikey+'&auth_token='+auth_token,}
payload = {'shared_link': {'access': 'Open'}}
r = requests.put(url, headers=headers, data=json.dumps(payload))
print r.content
return r.content

Moreover, the response object will let you access the actual request object that was sent over. So in the requests you were trying to make before, you can actually see the JSON that is being sent over by doing

r = requests.put(url, headers=headers, data=json.dumps(payload))
print r.request.data

Upvotes: 0

Aesthete
Aesthete

Reputation: 18848

Since you're just hardcoding your JSON anyway, why don't you just do this:

payload = '{"shared_link": {"access": "Open"}}'
r = requests.request("PUT", url, None, payload, headers)

Upvotes: 0

Related Questions