pythonpython-3.xrestpostpython-requests

using python requests to capture response when API call sends a please wait response before sending full response


I have an API call that sends the following response when the request is made through browser,

{'Status': {'Code': 4, 'Message': 'Please wait'}}

After a few seconds the response changes to

{'Status': {'Code': 0, 'Message': 'The request is successful', 'TransactionId': 456719}}

I can see this from the network tab in the browser's developer tools.

In python, if I execute the request twice while using sleep in between, I get the second response. But can I achieve this without executing the API call twice?


Solution

  • I figured out. For anyone who will encounter the same problem in the future, After making the call using requests, I increased the time.sleep before parsing the response and it worked. I had initially set it as time.sleep(5)

    response = requests.request("POST", url, json=req_json)
    time.sleep(10)
    response = response.json()
    

    I still need to wait 10s and can't make the wait time dynamic. So if I get the second response within 6s, i still waste 4s wasting, but I can live with this. If anyone has a better solution, please post here.