8

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.

2
  • 1
    What have you tried in python? What code? Requests has a section in the docs on authentication
    – Andy
    Commented Jan 5, 2018 at 16:59
  • You nailed it Andy. Hadn't seen that documentation. Thank you. Commented Jan 5, 2018 at 17:04

1 Answer 1

11

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.