view raw
Oli Oli - 6 months ago 35
Python Question

reading the response in python requests

I have 2 python scripts. One uses the urllib2 library and one uses the requests library. I have found requests much easier to implement but I can't seem to find the equivalent to read() when using the requests library. For example:

response = url.urlopen(req)
print response.geturl()
print response.getcode()
data =
print data

Once I have built up my post url, the data = gives me the content - I am trying to connect to a vcloud director api instance and the response shows the endpoints that I have access to. However if I use the requests library as follows.....


def post_call(username, org, password, key, secret):

endpoint = '<URL ENDPOINT>'
post_url = endpoint + 'sessions'
get_url = endpoint + 'org'
headers = {'Accept':'application/*+xml;version=5.1', \
'Authorization':'Basic '+ base64.b64encode(username + "@" + org + ":" + password), \
'x-id-sec':base64.b64encode(key + ":" + secret)}
print headers
post_call =, data=None, headers = headers)
print post_call, "POST call"
print post_call.text, "TEXT"
print post_call.content, "CONTENT"
post_call.status_code, "STATUS CODE"


....the print post_call.text and print post_call.content returns nothing evening though the status code equals 200 in the requests post call
Can anyone tell me why it's not returning any text or content?

Many thanks,


There isn't one.

>>> import requests
>>> response = requests.get("")
>>> print response.content
'<!doctype html><html itemscope="" itemtype=""><head>....'
>>> print response.content == response.text

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.