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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With