How do I read a response from Python Requests?
Requests doesn't have an equivalent to Urlib2's read()
.
>>> import requests
>>> response = requests.get("http://www.google.com")
>>> print response.content
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><head>....'
>>> print response.content == response.text
True
It looks like the POST request you are making is returning no content. Which is often the case with a POST request. Perhaps it set a cookie? The status code is telling you that the POST succeeded after all.Edit for Python 3:
Python now handles data types differently. response.content
returns a sequence of bytes
(integers that represent ASCII) while response.text
is a string
(sequence of chars).
Thus,
>>> print response.content == response.text
False
>>> print str(response.content) == response.text
True
how to get response data from a python post request
How about getting the content of the response?
For example:
print(response.text)
Or if you expect a JSON
:print(response.json())
How to extract the HTTP error text from a requests response?
You can access the response's status code using responsedata.status_code
and its textual description via responsedata.reason
(see more in http://docs.python-requests.org/en/master/api/)
How to extract HTTP response body from a Python requests call?
Your code is correct. I tested:
r = requests.get("http://www.google.com")
print(r.content)
And it returned plenty of content.Check the url, try "http://www.google.com".
How to get the raw content of a response in requests with Python?
If you are using a requests.get
call to obtain your HTTP response, you can use the raw
attribute of the response. Here is the code from the requests
docs. The stream=True
parameter in the requests.get
call is required for this to work.
>>> r = requests.get('https://github.com/timeline.json', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
Python requests - extracting data from response.text
You are receiving JSON; you already use the response.json()
method to decode that to a Python structure:
data = r.json()
You can treat data['uploaded']
as any other Python list; the content is just the one dictionary, so another dictionary key to get the id
value:data['uploaded'][0]['id']
It is safe to hardcode the index to [0]
here as you know how many images you uploaded.You could use exception handling to detect if anything unexpected was returned:
try:
image_id = data['uploaded'][0]['id']
except (IndexError, KeyError):
# key or index is missing, handle an unexpected response
log.error('Unexpected response after uploading image, got %r',
data)
or you could handle data['status']
; it all depends on the exact semantics of the API you are using here.
Related Topics
How to Reverse a Dictionary That Has Repeated Values
Python Pandas Dataframe, Is It Pass-By-Value or Pass-By-Reference
Importerror: Cannot Import Name Numpy_Mkl
Pip Is Not Able to Install Packages Correctly: Permission Denied Error
Importing Class from Another File
Python Element-Wise Tuple Operations Like Sum
Using Only the Db Part of Django
Differencebetween 'Py' and 'Python' in the Windows Terminal
Errors While Building/Installing C Module for Python 2.7
Strip/Trim All Strings of a Dataframe
How to Update SQLalchemy Row Entry
How to Frame Two for Loops in List Comprehension Python
Reading Tar File Contents Without Untarring It, in Python Script
How to Build a Systemtray App for Windows