Richard Richard - 1 month ago 7
Python Question

HTTP request equivalent of `curl --user` parameter?

I am retrieving events data from the Mailgun API. I am able to run this on the command line and get data back:

curl -s --user 'api:key-xxxx' https://api.mailgun.net/v3/mydomain.net/events


Now I want to script this with Python and requests. I know that it is possible to make a request using Basic-Auth to get the data via a URL. However, Mailgun says that API keys should be treated like a password, so I'd prefer not to expose API keys directly in the URLs if possible.

Is there any way I can do the equivalent of
curl --user
in requests, to supply the parameter but without exposing it in the URL?

Alternatively, would it be better to try to use
curl
directly inside Python?

Answer

just add the auth=('user', 'passwd') argument to the get method, it has to be a tuple with 2 strings.

my_req = requests.get('https://api.mailgun.net/v3/mydomain.net/events', auth=('user', 'pass')
Comments