response.text - Python requests
Last Updated : 15 Apr, 2025
In Python’s requests library, the response.text attribute allows developers to access the content of the response returned by an HTTP request. This content is always returned as a Unicode string, making it easy to read and manipulate. Whether the response body contains HTML, JSON, XML, or plain text, response.text offers a quick way to access and display it. Example:
Python import requests # Send a GET request r = requests.get('https://jsonplaceholder.typicode.com/posts/1') print(r.text)
Output
Using response.text()Explanation:
- requests.get() sends an HTTP GET request to the URL.
- r.text gives us the body of the response in string format.
- Even though the server returns JSON, response.text does not parse it — it just gives the raw text.
When to use response.text vs response.json()
When working with APIs or web pages using the requests library in Python, we receive different types of data in the server’s response such as plain text, HTML or JSON. It's important to understand which method to use (response.text or response.json()) based on the response format.
Use Case | Method | Returns | Example |
---|
Raw HTML or plain text data | response.text | str (string) | For web scraping HTML pages, reading error messages, etc. |
---|
JSON-formatted API response | response.json() | dict or list | For working with REST APIs that return structured data in JSON format. |
---|
Most common use cases of response.text
Example 1: We use the requests library to fetch a text file with 10,000 words from a URL. After receiving the response, we print the first 50 characters to preview the content. This demonstrates reading plain text files or static text APIs.
Python import requests r = requests.get('https://www.mit.edu/~ecprice/wordlist.10000') # Print first 50 characters print(r.text[:50])
Output
Example 1Example 2: We use the requests library to fetch data from an API. This is useful for logging or debugging raw API output, allowing us to inspect the server's response. We print the first 3 entries of the raw response text to quickly check the API's structure.
Python import requests # Get data from API r = requests.get('https://jsonplaceholder.typicode.com/posts') # Print first 3 entries from response text print(r.text.split('\n')[:3])
Output
Example 2Common pitfalls
While using the requests library in Python for handling HTTP requests, developers often fall into subtle traps that lead to unexpected behavior. Below are two such common pitfalls.
1. Incorrectly using response.text for JSON
Many developers use response.text to manually load JSON using json.loads(response.text). While this works in most cases, it's unnecessary and can introduce bugs if the response isn't properly encoded or if the content type isn’t exactly application/json.
Python import requests import json # Make a GET request r = requests.get("https://jsonplaceholder.typicode.com/posts/1") data = json.loads(r.text) print(data)
Output
Incorrectly using response.text for JSONExplanation: While this works, if the content type changes slightly or there are encoding issues, it might fail. Use r.json() instead.
2. Encoding issues- manually setting encoding
Manually setting r.encoding without understanding the actual encoding of the response can lead to incorrect rendering of the content, especially for non-UTF-8 text. The requests library typically infers encoding from the HTTP headers, and overriding it can cause issues.
Python import requests # Make a GET request r = requests.get("https://jsonplaceholder.typicode.com/posts/1") r.encoding = 'utf-8' print(r.text)
Encoding issues- manually setting encodingExplanation: In this case, it may still work, but forcibly setting encoding is not recommended unless you have verified the response encoding. Incorrectly setting it can corrupt the response text.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice
My Profile