Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to make a curl request with username/password

Tags:

python

curl

Trying to hit an api which returns a json. Documentation says to hit it in this fashion:

curl --user username:password "https://api.company.com/dummyapi.json"

Works like a champ from the command line and in PHP. I'm new to Python. How do I do mimic this in Python? I've tried requests and subprocess.call but I keep getting a bad request response from the server saying that credentials were not provided.

like image 686
Chris Utter Avatar asked Jan 05 '18 16:01

Chris Utter


People also ask

How do you send curl request with username and password?

To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.

How do you put authentication credentials in curl?

To use basic authentication, use the cURL --user option followed by your company name and user name as the value. cURL will then prompt you for your password.

How do I use the curl command to request?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. The target URL is passed as the first command-line option.


1 Answers

To do what curl does, in python you can use requests. First of all you have to install it :

pip install requests 

Then sending an http request is as easy as :

from requests import get
response = get('https://api.company.com/dummyapi.json', auth=('user', 'pass'))

Now response holds all information returned from that api. Use the documentation for more info, and in particular the section about authentication.

like image 72
Shahryar Saljoughi Avatar answered Nov 03 '22 00:11

Shahryar Saljoughi