I am using http.client to hit a POST api and get the json response. My code is working correctly when I print response.read(). However, for some reason, this response is limited to only 20 results and the total count of results is over 20,000. I want to get the complete response in a variable using response.read().decode(), I am hoping that the variable will contain the complete json string. The issue is that I am getting an empty string when I used decode(). How do I get this done? How do I get the complete results?
import http.client
host = 'jooble.org'
key = 'API_KEY'
connection = http.client.HTTPConnection(host)
#request headers
headers = {
"Content-type": "application/json"}
#json query
body = '{ "keywords": "sales", "location": "MA"}'
connection.request('POST','/api/' + key, body, headers)
response = connection.getresponse()
print(response.status, response.reason)
print(response.read())
print(response.read().decode())
Don't call
response.read()twice.responseis a stream, so each call toread()continues from where the previous one ended. Since the first call is reading the entire response, the second one doesn't read anything.If you want to print the encoded and decoded response, assign
response.read()to a variable, then decode that.But this can be done more simply using the
requestsmodule.Note that in this version
bodyis a dictionary, not a string. Thejsonparameter automatically converts it to JSON.