shibs
shibs

Reputation: 53

Convert from curl python Request Error error 401

Im having trouble converting the curl request into a python code request.

Working Curl request

curl -X POST "http://xxxxxx" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Token 882a6ec053ff6dbac623eff400f67c0bb6ade399" -d "name=namename"

Not working python request

headers = {
    'Authorization ': 'Token ' + "token",
    'Content-Type': 'application/json',
}
data= {'name': "name"}
r = requests.post(
    host_scheme + "://" + host_netloc + "/xxxxx",
    data=json.dumps(data),
    headers=headers
)

The response of the error is it cannot read the token {"detail": "Authentication credentials were not provided."} when using the python code above.

Any suggestions?

Upvotes: 2

Views: 1147

Answers (1)

paragbaxi
paragbaxi

Reputation: 4243

requests.post("http://xxxxxx",
    data='name=namename',
    headers={
        "Authorization": "Token 882a6ec053ff6dbac623eff400f67c0bb6ade399",
        "Content-Type": "application/x-www-form-urlencoded",
        "accept": "application/json"
    },
    cookies={},
)

I used Uncurl. I had to remove the -X POST.

Upvotes: 2

Related Questions