Convert from curl python Request Error error 401

Question:

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?

Asked By: shibs

||

Answers:

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.

Answered By: paragbaxi

For future reference, same issue but due to netrc file. Python requests library decided to override the Authorization header if a netrc matching entry is found. https://requests.readthedocs.io/en/latest/user/authentication/#netrc-authentication

Answered By: Fred Simon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.