Reputation: 31
I'm a beginner and learning Python. I wanted to write a python code to "call an REST API". Can you please guide me the steps to call an Rest API?
Also I've curl command which I wanted to write it in python code.
curl -u username:password -X GET 'http://google.com:8070/api/v2/organizations'
It would much helpful if anyone can help me out.
Thanks
Upvotes: 2
Views: 30410
Reputation: 2818
The best option for you is possibly using requests
package.
install it with pip install requests
.
An exapmple taken from their website is:
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
For further info check its documentation.
Upvotes: 6
Reputation: 113930
import requests
from requests.auth import HTTPBasicAuth
requests.get("http://google.com:8070/api/v2/organizations",auth=HTTPBasicAuth('user', 'pass'))
you should really read some docs and write some code... these things become really obvious with some very quick googling (ie "python http authentication request")
http://docs.python-requests.org/en/master/user/authentication/
Upvotes: -5