Nir
Nir

Reputation: 31

Call REST API with authentication using Python

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

Answers (2)

Farzad Vertigo
Farzad Vertigo

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

Joran Beasley
Joran Beasley

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

Related Questions