'Custom JSONEncoder for requests.post
I'm writing wrapper for REST API and use requests module.
Method .json()
of Response object transfers **kwargs
to json.loads()
function, so I can easily use custom JSON decoder and, i. e. transparently convert UNIX epoch timestamps to datetime.datetime objects.
Is there any way to use custom JSON encoder with Request object? Seems I can only use parameter json, but can't find how to use custom JSON encoder with it.
Solution 1:[1]
Extracting the answer from the link provided by alecxe, using a custom encoder and the json parameter is not supported. It's recommended you just construct the post manually.
r = requests.post('http://foo.bar',
data=json.dumps(some_data, cls=CustomJSONEncoder),
headers={'Content-Type': 'application/json'})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |