小编Rod*_*nde的帖子

Python 中更新请求响应内容

我是Python新手。我正在尝试使用请求库对在交换响应中获得的 Json 正文进行更改。

我想做类似的事情:

import json
import requests

def request_and_fill_form_in_response() -> requests.Response():
    response = requests.get('https://someurl.com')
    body_json = response.json()
    body_json['some_field'] = 'some_value'
    response.content = json.dumps(body_json)
    return response
Run Code Online (Sandbox Code Playgroud)

在这个特定的场景中,我只对更新 response.content 对象感兴趣(无论这是否是一个好的做法)。

这可能吗?

(顺便说一句,上面的代码抛出'AttributeError: can't set attribute'错误,这几乎是不言自明的,但我想确保我没有遗漏一些东西)

python json python-requests

3
推荐指数
1
解决办法
2651
查看次数

标签 统计

json ×1

python ×1

python-requests ×1