Last modified: Jul 02, 2023 By Alexander Williams

How to Get requests Status Code

In this tutorial, we're going to learn how to check status code in the request's library.

Syntax


requests.get(url).status_code

Example


import requests

url = "https://pytutorial.com/"

res = requests.get(url)

print(res.status_code)

Output

200

As you can see, we've used get method to send a get request then print the status code.

Example with function

Now let's rewrite the above code with function.


def check_status_code(url):

    res = requests.get(url)

    return res.status_code


How to use it?


check_status_code("https://pytutorial.com/no-exist")

Output

404