#๐Ÿ”’ How do I check the status of an API that I access with post requests without a "test" request?

14 messages ยท Page 1 of 1 (latest)

proper elbow
#

this is my current simple function

def endpoint_is_valid(url: str) -> bool:
    try:
        response = requests.post(
                    url, 
                    json={
                        # here I have some dummy data
                    }
                )
        return response.status_code == 200
    except requests.exceptions.RequestException:
        return False

in this function I am doing a "test" request to check if the request is successful so I can make further requests in the future

however, since i just wanna check if this url is available, I think that this code is not good, since I send nonsense data

I tried to make a get request, however I dont get response code 200,

I have multiple urls where I have to check their availability so the solution should be applicable more generally

surreal mantleBOT
#

@proper elbow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pale night
#

I'm confused as to why you would want to do this "pre check", maybe i'm just being dense but why wouldn't you just make the request, and then deal with it if it fails. Why the need to pre check? if you pre check, and the end point works, whats to say it doesn't work immediately after your pre-check

#

In any case you could make a header request, which will tell you if the endpoint is reachable.

proper elbow
pale night
#

Honestly don't understand what you're saying 100%.

#

If I am understanding correctly, I don't see any issue with that approach?

#

whats your actual issue with it

proper elbow
# pale night Honestly don't understand what you're saying 100%.

I rephrase

I have a program that needs to use an endpoint several times in the course of the program. These individual requests are executed in separate threads. If one of these requests fails, all other requests are useless to me.

So if I make, say, 12 individual requests without checking the API first, and one of them fails, I have to tell all the other threads not to make any more requests.

Let's say I have the status code 429, so every time the program is restarted, unnecessary time is wasted at the beginning to start these threads, which in the end have no use, because with the code 429 I can't make any more requests for a while, and my program tries to make several requests every time it is restarted, but they fail anyway. My idea is to solve this problem by first requesting the URL.

proper elbow
proper elbow
surreal mantleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.