pythonpython-3.xpython-requestshttp-headersdeviantart-api

python-requests: What is different here between my python script and their curl command?


There is some api endpoint, which i try to send POST request with code like this

import requests
url = 'https://deviantart.com/api/v1/oauth2/collections/folders/create'
headers = {'Content-Type': 'application/json'}
data = {'folder': 'folder_name'}
params = {'access_token': '<authorization_code_flow_token>'}
r = requests.post(url=url,
                  headers=headers,
                  params=params,
                  json=data)
print(r.text)

But i get 400 response:

{
  "error":"invalid_request",
  "error_description":"Request field validation failed.",
  "error_details":{"folder":"folder is required"},
  "status":"error"
}

I don't understand why it fails, because their example with curl works fine.

curl https://www.deviantart.com/api/v1/oauth2/collections/folders/create \
-d "folder=Awesome Collection" \
-d access_token=Alph4num3r1ct0k3nv4lu3

And I was successful with post responses to get authenticated.
I tried to change content-type header(json and x-www-formurlencoded) and pass data-payload different ways(passing json string to data param, passing dict to json, passing paylod as query string). But It does not work. I dont have a clue what i am doing wrong. It seems like i send payload wrong or put wrong headers, but i tried a lot of "combinations" and still no effect.

For next hour you if you want try to help you can use access_token: ba4550889c8c36c8d82093906145d9fd66775c959030d3d772


Solution

  • The following code will work.

    import requests
    
    url = "https://www.deviantart.com/api/v1/oauth2/collections/folders/create"
    
    payload={'folder': 'Awesome Collection',
    'access_token': 'ba4550889c8c36c8d82093906145d9fd66775c959030d3d772'}
    files=[]
    headers = {}
    
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    
    print(response.text)