1

This works from the command line:

 curl -H "Content-Type: application/json" -X POST -d '<my data>' http://my_user:my_pass@my_url

This doesn't work from a python script:

 res=requests.post(
  'http://my_user:my_pass@my_url',  
  json='<my data>')

What happens is it hits the server, but doesn't authorize. The REST API is built with Django Rest Framework, and I get

{"detail":"Invalid username/password."}

http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/

Password includes these special characters % ( \

I escaped \ , so it's a double backslash. I also tried with r in front of string, and 4 backslashes.

I tried with auth=('my_user','my_pass') with the different escapes too.

I ran it through http://curl.trillworks.com/ and still that didn't work.

Tomorrow I'm going to change my password to something simple and test.

If that doesn't work, I'm giving up and adding a bash script at the end that just runs that curl command.

2
  • use auth param for passing credentials. Commented Aug 30, 2016 at 11:52
  • What are you passing as data and what usr are you using when you use auth=...? Commented Aug 30, 2016 at 22:10

2 Answers 2

2

You need to try setting the authentication using requests library like this:

from requests.auth import HTTPBasicAuth

response = requests.post(url, json=payload, auth=HTTPBasicAuth('user', 'pass'))

http://docs.python-requests.org/en/master/user/authentication/#basic-authentication

2
  • auth=('user', 'pass') is exactly the same as importing HTTPBasicAuth and doing it yourself which the OP already tried according to their question so there should be no difference. Commented Aug 30, 2016 at 22:07
  • Yes, both worked. My problem was the backslash in the curl statement was to escape the parenthesis, and not part of the password itself.
    – stampede76
    Commented Sep 2, 2016 at 12:27
0

I escaped \ , so it's a double backslash. I also tried with r in front of string, and 4 backslashes.

The \ in the curl command was to escape a parenthesis. It wasn't part of the password.

I removed it and now it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.