python requests.models.response

Python Requests.models.response

Python Requests is a widely used HTTP client library that allows users to send HTTP/1.1 requests in Python. The 'requests.models.response' module in Python Requests represents the server's response to an HTTP request.

The Structure of a Response Object

A response object is created when a request is made to a server using Python Requests. The response object contains several attributes and methods that provide information about the response received from the server. Some of the commonly used attributes and methods of the 'response' object are:

  • status_code: The HTTP status code returned by the server.
  • headers: A dictionary containing the response headers.
  • text: The response text as a string.
  • content: The response content in bytes.
  • json(): A method that returns the response content as a JSON object.

Example Code


import requests

response = requests.get('https://www.example.com')
print(response.status_code)
print(response.headers)
print(response.text)
print(response.content)
print(response.json())

Multiple Ways to Receive the Response Content

The response content can be received in multiple ways depending on the type of data and user preference. Some of the commonly used methods to receive response content are:

  • text: Returns the content in Unicode format.
  • content: Returns the content in bytes.
  • json(): Returns the content in JSON format.
  • raw(): Returns the raw response content as a file-like object.

Example Code


import requests

response = requests.get('https://www.example.com')
print(response.text)
print(response.content)
print(response.json())
print(response.raw())