English 中文(简体)
Handling Response for HTTP Requests
  • 时间:2024-11-03

Handpng Response for HTTP Requests


Previous Page Next Page  

In this chapter, we will get into more details of the response received from the requests module. We will discuss the following details −

    Getting Response

    JSON Response

    RAW Response

    Binary Response

Getting Response

We will make a request to the URL using request.get() method.


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users );    

The getdata has the response object. It has all the details of the response. We can get a response in 2 ways using (text) and (.content). Using response.text will give you the data back in text format as shown below −

Example


E:prequests>python makeRequest.py
[
   {
      "id": 1,
      "name": "Leanne Graham",
      "username": "Bret",
      "email": "Sincere@april.biz",
      "address": {
         "street": "Kulas Light",
         "suite": "Apt. 556",
         "city": "Gwenborough",
         "zipcode": "92998-3874",
        "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
         }
      },
      "phone": "1-770-736-8031 x56442",
      "website": "hildegard.org",
      "company": {
         "name": "Romaguera-Crona",
         "catchPhrase": "Multi-layered cpent-server neural-net",
         "bs": "harness real-time e-markets"
      }
   },
]

You will see the response is the same, as how it would have appeared in the browser when you do view source for the URL as shown below −

Typicode

You can also try out .html URL and see the content using response.text, it will be the same as the view source content for the .html URL in the browser.

Now, let us try response.content for the same URL and see the output.

Example


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users )
print(getdata.content)

Output


E:prequests>python makeRequest.py
b [
 {
 "id": 1,
 "name": "Leanne Graham",
 "username": "Bret",

"email": "Sincere@april.biz",
 "address": {
 "street": "Kulas Light
",
 "suite": "Apt. 556",
 "city": "Gwenborough",
 "zipcode": "
92998-3874",
 "geo": {
 "lat": "-37.3159",
 "lng": "81.149
6"
 }
 },
 "phone": "1-770-736-8031 x56442",
 "website": "hild
egard.org",
 "company": {
 "name": "Romaguera-Crona",
 "catchPhr
ase": "Multi-layered cpent-server neural-net",
 "bs": "harness real-time
e-markets"
 }
 },
 {
 "id": 2,
 "name": "Ervin Howell",

"username": "Antonette",
 "email": "Shanna@mepssa.tv",
 "address": {

"street": "Victor Plains",
 "suite": "Suite 879",
 "city": "Wisoky
burgh",
 "zipcode": "90566-7771",
 "geo": {
 "lat": "-43.950
9",
 "lng": "-34.4618"
 }
 },
 "phone": "010-692-6593 x091
25",
 "website": "anastasia.net",
 "company": {
 "name": "Deckow-Crist",

 "catchPhrase": "Proactive didactic contingency",
 "bs":
"synergize scalable supply-chains"
 }
 },
 {
 "id": 3,
 "name": 
"Clementine Bauch",
 "username": "Samantha",
 "email":
"Nathan@yesenia.net",

 "address": {
 "street": "Douglas Extension",
 "suite": "Suite
847",
 "city": "McKenziehaven",
 "zipcode": "59590-4157",
 "ge
o": {
 "lat": "-68.6102",
 "lng": "-47.0653"
 }
 },


The response is given in bytes. You will get a letter b at the start of the response. With the requests module, you can get the encoding used and also change the encoding if required. For example, to get the encoding you can use response.encoding.


print(getdata.encoding)

Output


utf-8

You can change the encoding as follows − You can use the encoding of your choice.


getdata.encoding =  ISO-8859-1 

JSON Response

You can also get the response for the Http request in json format by using response.json() method as follows −

Example


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users )
print(getdata.json())

Output


E:prequests>python makeRequest.py
[{ id : 1,  name :  Leanne Graham ,  username :  Bret ,  email :  Sincere@april.
biz ,  address : { street :  Kulas Light ,  suite :  Apt. 556 ,  city :  Gwenborough , 
 zipcode :  92998-3874 ,  geo : { lat :  -37.3159 ,  lng :  81.1496 }},
 
phone :  1-770-736-8031 x56442 ,  website :  hildegard.org ,  company : { name :
 Romaguera-Crona ,  catchPhrase :  Multi-layered cpent-server neural-net ,  bs : 
 harness real-time e-markets }}]

RAW Response

In case you need the raw response for the Http URL you can make use of response.raw, also add stream = True inside the get method as shown below −

Example


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users , stream=True)
print(getdata.raw)

Output


E:prequests>python makeRequest.py
<urlpb3.response.HTTPResponse object at 0x000000A8833D7B70>

To read for more content from the raw data you can do so as follows −


print(getdata.raw.read(50))

Output


E:prequests>python makeRequest.py
b x1fx8bx08x00x00x00x00x00x00x03x95x98[oxe38x12x85xdfxe7Wx10y
xdax01Fx82.xd4mx9fxdcx9ddxbaxb7x93xf4x06qxef4x06x83A@Kx15x89m 

Binary Response

To get a binary response we can make use of response.content.

Example


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users )
print(getdata.content)

Output


E:prequests>python makeRequest.py
b [
 {
 "id": 1,
 "name": "Leanne Graham",
 "username": "Bret",

"email": "Sincere@april.biz",
 "address": {
 "street": "Kulas Light
",
 "suite": "Apt. 556",
 "city": "Gwenborough",
 "zipcode": "
92998-3874",
 "geo": {
 "lat": "-37.3159",
 "lng": "81.149
6"
 }
 },
 "phone": "1-770-736-8031 x56442",
 "website": 
"hildegard.org",
 "company": {
 "name": "Romaguera-Crona",
 "catchPhr
ase": "Multi-layered cpent-server neural-net",
 "bs": "harness real-time
e-markets"
 }
 },
 {
 "id": 2,
 "name": "Ervin Howell",
 "us
ername": "Antonette",
 "email": "Shanna@mepssa.tv",
 "address": {

"street": "Victor Plains",
 "suite": "Suite 879",
 "city": "Wisoky
burgh",
 "zipcode": "90566-7771",
 "geo": {
 "lat": "-43.950
9",
 "lng": "-34.4618"
 }
 },
 "phone": "010-692-6593 x091
25",
 "website": "anastasia.net",
 "company": {
 "name": "Deckow-Crist",

 "catchPhrase": "Proactive didactic contingency",
 "bs": "syn
ergize scalable supply-chains"
 }
 },
 {
 "id": 3,
 "name":
"Clementine Bauch",
 "username": "Samantha",
 "email": "Nathan@yesenia.net",

 "address": {
 "street": "Douglas Extension",
 "suite": "Suite
847",
 "city": "McKenziehaven",
 "zipcode": "59590-4157",
 "
geo": {
 "lat": "-68.6102",
 "lng": "-47.0653"
 }
 },


The response is given in bytes. You will get a letter b at the start of the response. The binary response is mostly used for non-text requests.

Advertisements