1

The curl command that I have that works properly is -

curl -X GET -H "Authorization: Basic <base64userpass>" -H "Content-Type: application/json" "http://<host>/bamboo/rest/api/latest/result/<plankey>.json?expand=results.result&os_authType=basic"

In Python, this is what I currently have -

   headers = {'Authorization': 'Basic <base64userpass>', 'Content-Type': 'application/json'}
   datapoints = {'expand': 'results.result', 'os_authType': 'basic'}
   url = "http://<host>/bamboo/rest/api/latest/result/<plankey>.json"
   r = requests.get(url, headers=headers, data=datapoints)

The response I get when using the Python request is <Response [403]>, but when using curl I get back the expected data.

What am I missing here?

Thanks.

1
  • you need params= instead of data= Commented Dec 6, 2016 at 1:10

1 Answer 1

5

You should use the auth option of requests to do basic authentication. There are more headers that the CURL command-line handle for you (and requests will not handle them unless you use the auth):

>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))

Or just use:

>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))

(Change the URL and everything).

Also note that requests should get the params= (and not data=).

Sign up to request clarification or add additional context in comments.

5 Comments

The only concern that I have with this is that the only value that I currently have is that base64 encoded value (lets say it is CmUnMm9dOkJGhaUIwBaMlNTYyJh=), and not the user/pass, would it be possible to use this method still?
Are you sure this is the value?
I just made up a series of numbers in that example, but yes I am sure that the series of numbers I am actually using is the username / password. It has worked for other scripts in the system. I suppose I could just decode it, but I was hoping that it would work without me having to have a password in plaintext inside of the script.
basically this string is just base64_encode(username:password) so what you can do is base64_decode on that string to get the username and password.
This answers also work for my case which is convert HTTPie command to Python Requests, thanks Man :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.