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, ie 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.


Extracting the answer from the link provided by alecxe (https://github.com/kennethreitz/requests/issues/2755), 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'})
链接地址: http://www.djcxy.com/p/89378.html

上一篇: 在功能或课堂之外的“超级”

下一篇: 为requests.post自定义JSONEncoder