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

Hint: You can adjust the default video playback speed in your account settings.

Responses: Content and Headers

Give Feedback

When dealing with requests we are mostly interested in the content we receive. In this lesson you’ll learn how to access the response content as well as the response meta information stored in the headers.

You can access the content of a response in three different ways and formats. Here is a quick overview about the different ways and formats:

  • content: This attribute returns the raw bytes of the response content
  • text: The text attribute returns the content as a normal UTF-8 encoded Python string
  • json(): You can use the json() method to get the response content in JSON format

The headers attributes returns a Python dictionary with all of the response headers.

Comments & Discussion

Abby Jones on June 25, 2019

I am learning SO much about Python, VIM, and now bpython. RealPython is literally the best site I have ever learned from. It’s increasing my workflow productivity.

Pygator on Sept. 15, 2019

how can a dictionary have multiple keys that produce the same value or are they duplicated? Doesn’t that break all universal laws of python dicts?

km on Dec. 6, 2019

Ending with JSONDecodeError: Expecting value, used url=’www.ucla.edu‘ - Any help

Chris Bailey RP Team on Dec. 6, 2019

Hi Kishoremarodia, The url you are suggesting is not an API end point, and returns a response of html elements instead of JSON. Is there an API you would like to explore at ucla.edu?

km on Dec. 7, 2019

Thanks a lot, I just took it randomly. Is there any way to find out an END POINT API which can send elements in JSON? The API.github.com is rejecting requests.

Chris Bailey RP Team on Dec. 9, 2019

Hi km, I just tried the example in the lesson, and it responded the same as in the lesson. Maybe its the number of times you have tried it, hopefully that would reset after a while. If you are trying some of the later lessons, you would need an account on github, and then would use your credentials. You could also try some from API’s this reddit thread. Many of them will require you to make an account.

balakumaranrk on Feb. 8, 2020

what is the difference between response.json() and response.content ? Both of them fetch key-value pairs right ?

Ricky White RP Team on Feb. 8, 2020

Not exactly. response.content will give you the raw data which you can parse into json or a dictionary object easily.

Become a Member to join the conversation.