Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Responses: Status Codes

Give Feedback

What are HTTP status codes? Which status codes do exist? These questions are answered in this lesson of the Making HTTP Requests With Python course. You’ll learn about the different status code ranges and what they mean internally. Additionally, you’ll learn how to handle different status codes in multiple ways, e.g. by using if-statements or try-except-blocks.

Comments & Discussion

balakumaranrk on Feb. 8, 2020

In this video what is the purpose of writing :

response.raise_for_status()

can you please explain ?

Ricky White RP Team on Feb. 8, 2020

This is used so that no exception is raised on a successful request. This will allow you to capture unsuccessful requests based on the exception type returned and deal with them accordingly.

idosegev on Feb. 12, 2020

I don’t get any response from my if-elif loop and i don’t know why. maybe it has to do with python38 ?

import requests

url = ‘api.github.com‘ response = requests.get(url)

if response.status_code == 202: print(‘Success!’) elif response.status_code == 404: print(‘Not Found.’)

Chris Bailey RP Team on Feb. 12, 2020

Hi @idosegev,

There are a couple of differences from your code in the comment you posted and the example in the video lesson:

The url should be url = 'http://api.github.com' The first if should be if response.status_code == 200: and not 202. 200 would mean success and 202 would mean something different, accepted. Because the code I’m demonstrating is an incomplete example, I don’t have a fall back of else: which would catch some other result. I guess you could have it print the response, such as else: print(f'Status Code was {response.status_code}')

Using an f string. I hope this helps with your question.

idosegev on Feb. 13, 2020

@Chris Bailey thanks for the quick response!

Become a Member to join the conversation.