Custom JSONEncoder for request.post

I am writing a wrapper for the REST API and using requests .

The .json() Response method transfers the **kwargs object to json.loads() , so I can easily use a custom JSON decoder and, i. e. transparently convert UNIX-era timestamps to datetime.datetime objects.

Can I use my own JSON encoder with Request object? It seems that I can only use the json parameter, but I cannot find how to use the custom JSON encoder with it.

+7
python python-requests
source share
1 answer

Extracting the response from the link provided by alecxe ( https://github.com/kennethreitz/requests/issues/2755 ), using a custom encoder and json parameter is not supported. He recommended that you simply create the message manually.

 r = requests.post('http://foo.bar', data=json.dumps(some_data, cls=CustomJSONEncoder), headers={'Content-Type': 'application/json'}) 
+3
source share

All Articles