Home > OS >  Why do I get timeout error doing an api call using python with requests library if with curl works w
Why do I get timeout error doing an api call using python with requests library if with curl works w

Time:05-23

I trying to make a simple call to an api. With this instruction of curl in the terminal works fine, I get the json file back

curl -X GET "https://my.domain.com/api/product/?name=product" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -H "x-api-key: 02987eafdeef73450982734"

I trying to make the same call in python with the requests library, but I always receive an timeout error:

base_url = 'https://my.domain.com'
timeout = 10
headers = {
    'content-type': 'application/json',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'x-api-key': '02987eafdeef73450982734'
}

results = requests.get(
    f'{base_url}/api/product/?name=product',
    headers=headers,
    timeout=timeout
) # a headers param exists

Can you see the reason? Am I doing anything wrong? Am I missing anything?

CodePudding user response:

It appears that the default timeout of curl is 5 minutes (AskUbuntu). My first suggestion would be to increase the timeout of the request using the requests package to 5 minutes (timeout = 5 * 60) and see if the request succeeds.

To answer your question directly, the requests look sufficiently similar. You might also want to try making the request using a GUI client (e.g. Hoppscotch); it'll tell you the response time e.t.c.

If you only see the timeout-style errors from requests, try different Python libraries for making HTTP requests: it might help you narrow down the issue.

CodePudding user response:

I was not setting the api url well in python. I was not using the same as I was posting in my question.

Enabling the verbose mode helped me to find the error, in the request library. This way:

import logging
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# request GET call

To enable the verbose mode in curl I just needed to add the -v parameter.

  • Related