Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Requests · Const

status_code

Response.status_code is the integer HTTP status returned by a server, such as 200 or 404. Use it for exact status checks, optionally comparing it with a named value from requests.codes.

How it works

A response's status_code is the integer HTTP status returned by the server, such as 200 or 404. Read it directly for exact comparisons, or compare it with a named value from requests.codes when that improves readability.

Syntax

Response.status_code

Returns

int

Key points

  • status_code stores the server's HTTP response status as an integer.
  • Inspect the property after a request completes to determine the response's HTTP result.
  • Named alternatives are available through the requests.codes lookup object.
  • Status names can be retrieved with attribute syntax or dictionary indexing.
  • The lookup accepts aliases and permits either uppercase or lowercase status-name forms.

Common mistakes

  • Do not treat Response.ok as an exact 200 check; status_code itself is the appropriate property for testing a particular code.

Example: Read a response status code

import requests

response = requests.get("https://httpbin.org/status/204")
print(response.status_code)

if response.status_code == requests.codes.no_content:
    print("No content")

Fetches a response, reads its integer HTTP status, and compares it with the named Requests status-code constant.

When to use it

Check an exact response status

After any HTTP request, inspect whether the server returned the expected status.

Compare with a status constant

Use a readable named status instead of embedding a numeric literal.

Handle response categories

Branch application behavior for success, redirection, client-error, or server-error responses.

Interview questions

QHow do you inspect an HTTP response's status code?

Read the integer directly from the Response object: response.status_code. The documentation describes it as the returned HTTP status code and gives values such as 404 and 200.

Follow-up: How can a named status value improve readability? Compare the integer with a corresponding requests.codes attribute, such as requests.codes.ok.

QHow do you test for a successful 200 response?

Use an exact comparison, for example response.status_code == 200. Requests also provides named constants through requests.codes, so requests.codes.ok can represent 200.

Follow-up: What should you use when any client or server error should fail? Call response.raise_for_status(), which raises HTTPError when an error occurred.

QDoes reading status_code raise for HTTP errors?

status_code is an integer property on the Response object; it does not itself raise an exception for an error response. Error handling is separate and can be performed with raise_for_status().

Follow-up: Why might status_code be preferable to ok? It distinguishes specific outcomes, while ok is true for every status below 400.

  • Response.ok
  • Response.raise_for_status()
  • requests.codes
  • Response

Source: Developer Interface — Requests 2.34.2 documentation. Checked 2026-10-15.