Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify a Python "requests" module response object

I'm requesting an HTTP API with the Python "requests" module.

result = requests.get(api_url)

The response is in JSON format and contains a 'data' key which is a dictionary. For some reasons, I have to make a second request further in the script and I'm trying to update the first dictionary (result.json['data']) with the second (result2.json['data']).

I tried this:

result.json['data'].update(result2.json['data'])

and this:

for key,value in dict(result2.json['data']).iteritems():
    result.json['data'][key] = value

but none of these worked, the final result.json['data'] is not modified and only contains the first items.

So I'm wondering if the Python "Requests" module produces some king of "read-only" objects or if I am just missing something.

like image 622
gr0bz Avatar asked Oct 13 '14 14:10

gr0bz


People also ask

How do you change a request-response to a json in Python?

you can turn it into JSON in Python using the json. loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary.

How do you respond to an object in Python?

When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests. method(), method being – get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of code.

What type of object is returned by requests get ()?

The get() method returns a requests. Response object.


1 Answers

result.json() is either a method (requests 1.0 and up) or a property. Store the result of that method or property in a variable, then update that:

json_result = result.json()
# requests < 1.0: json_result = result.json

json_result['data'].update(result2.json()['data'])

(In older requests versions (pre-1.0), result.json is a property, and underneath that is still a method returning your object on the fly).

Otherwise, Response objects are not meant to be mutable. You'd have to alter the response body text to alter what result.json returns, and it would require intimate knowledge of how the Response objects cache the response body to alter that. This would tie you to a specific requests version.

For the current requests series (2.4.x), you can swap out the response._content attribute:

result.encoding, result._content = 'utf8', json.dumps(json_result)

where I've made the assumption that result.content already exists (e.g. a streaming response has already been consumed), and that json_result has already been updated.

If this will work in other requests versions depends; _content is an internal, private attribute. The leading underscore indicates it is not part of the public API, so the developers are free to change the meaning of that attribute or altogether remove it in future releases.

like image 111
Martijn Pieters Avatar answered Sep 19 '22 14:09

Martijn Pieters