python - requests.post 的自定义 JSONEncoder

标签 python python-requests

我正在为 REST API 编写包装器并使用 requests模块。

方法 .json() Response对象将 **kwargs 传输到 json.loads() 函数,因此我可以轻松地使用自定义 JSON 解码器,我。 e.透明地将 UNIX 纪元时间戳转换为 datetime.datetime 对象。

有什么方法可以将自定义 JSON 编码器与 Request 一起使用吗?目的?似乎我只能使用参数 json,但找不到如何使用自定义 JSON 编码器。

最佳答案

link provided by alecxe 中提取答案,使用自定义编码器且不支持 json 参数。建议您手动构建帖子。

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

关于python - requests.post 的自定义 JSONEncoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366405/

相关文章:

python - seleniumexecute_script() 返回错误值

python - numpy 中不同大小数组的乘积

python - 我对此类网站的 POST 感到困惑

python - 如何使用 Python 请求在 Amazon S3 上执行 `PUT`

python - 在 Python 中映射名称

python - 如何定义引用其自身模型的 django 外键 limit_choices_to 约束?

python - 可以使用 exec 运行异步功能吗?

django - 有没有最简单的方法来异步运行多个python请求?

python - Locustio(Python)登录

python - "Memory Leak"与 grequests?