HTTP Status Code Fetcher

  • Share this:

Code introduction


This function is used to get the status code of an HTTP request. If the request is successful, it returns the status code; if the request fails, it returns an error message.


Technology Stack : httpx

Code Type : HTTP request

Code Difficulty : Intermediate


                
                    
def fetch_random_status_code(url):
    from httpx import HTTPClient, Request, Response
    client = HTTPClient()
    try:
        response: Response = client.get(url)
        return {"status_code": response.status_code}
    except Exception as e:
        return {"error": str(e)}                
              
Tags: