我的头部格式如下
import requests
header = {
"content-type": "application/json",
"a": {"b": "b_value"},
"some_param": {"some_key_value": "some_string"},
}
res = requests.post(url, json=z_data, headers=header)我得到一个错误,字典的值不能是字典。我尝试使用json.dumps(),但它忽略了其中的内容。
请帮助我解决这个错误。谢谢,
发布于 2020-03-24 01:50:39
您必须对参数进行json编码:
header = {
"content-type": "application/json",
"a": json.dumps({"b": "b_value"}),
"some_param": json.dumps({"some_key_value": "some_string"})
}如果您收到预先构建的header字典,则必须对其进行预处理:
header = {
"content-type": "application/json",
"a": {"b": "b_value"},
"some_param": {"some_key_value": "some_string"},
}
...
header = {k: json.dumps(v) if isinstance(v, dict) else v for k,v in header.items()}
res = requests.post(url, json=z_data, headers=header)发布于 2020-03-24 01:20:08
看起来参数应该是类似json的字符串。使用json模块应该可以,但您可以尝试使用f字符串:
header = {
"content-type": "application/json",
"a": f'{{"b": {b_value} }}',
"c": f'{{"d": {d_value} }}',
}这样,您的变量a和c就是字符串,变量b_value和d_value将被替换为其内容。
https://stackoverflow.com/questions/60818348
复制相似问题