0% found this document useful (0 votes)
31 views2 pages

Third Presentation

The document discusses using the Python requests library to make an API GET request with authentication, as is normally done using cURL. It provides an example cURL command using authentication headers. The author is trying to replicate this in Python using requests but is getting a 401 error when including authentication headers. They are looking for help getting the authentication to work in the requests code.

Uploaded by

Adrian Marinaș
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Third Presentation

The document discusses using the Python requests library to make an API GET request with authentication, as is normally done using cURL. It provides an example cURL command using authentication headers. The author is trying to replicate this in Python using requests but is getting a 401 error when including authentication headers. They are looking for help getting the authentication to work in the requests code.

Uploaded by

Adrian Marinaș
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

I am trying to write a Python script that will allow me to accomplish what I normally would by using

CURL to perform an API "GET". I have browsed through some questions on here but I am still a bit
confused on how to do this; from what I have seen, I can use the "requests" library like this:

URL = requests.get("www.example.com/123")

However, the Curl command I normally run uses authentication as well. Here is an example:

curl -X GET -H 'Authorization: hibnn:11111:77788777YT666:CAL1' 'http://api.example.com/v1.11/user?


id=123456

Can I still use the requests library when creating a script to pull this data? Specifically, I would need to be
able to pass along the authentication info along with the URL itself.

Update:

Here is what my code looks like:

import requests

import json

url = ("api.example.com/v1.11/user?id=123456")

header = {"Authorization": "hibnn:11111:77788777YT666:CAL1"}

response = requests.get(url, headers=header)

print(response.json)

However, I am getting a response [401] error. I think I am probably doing something wrong with my
headers, can anyone help?

pythonpython-2.7

Share

Improve this question

Follow
edited Dec 28, 2020 at 12:49

jeppoo1's user avatar

jeppoo1

58211 gold badge88 silver badges1818 bronze badges

asked Mar 22, 2017 at 5:21

user7681184's user avatar

user7681184

76311 gold badge1111 silver badges2424 bronze badges

Add a comment

1 Answer

Sorted by:

Highest score (default)

You may pass headers as keyword argument to the get method.

>>> url = 'https://api.github.com/some/endpoint'

>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)

You might also like