我有一个 python 代码,它应该创建一个 json 文件,用作我发布请求中的数据。
变量看起来像这样:
request="""{"QueryString":"{\\"search_type\\":5,\\"project_id_list\\":[\\"1\\"],\\"user_key\\":\\"a\\",\\"options\\":{\\"pagination\\":{\\"page_num\\":1,\\"page_size\\":200},\\"projection\\":{\\"name\\":1},\\"condition_group\\":{\\"conjunction\\":\\"AND\\",\\"conditions\\":[],\\"groups\\":[]},\\"facet\\":{\\"fields\\":[],\\"type\\":0,\\"update_one\\":false},\\"groups\\":[],\\"sort\\":[]},\\"search_types\\":[]}","SearchType":4}"""
当我打印上述变量时,我得到以下输出:
{"QueryString":"{\"search_type\":5,\"project_id_list\":[\"1\"],\"user_key\":\"a\",\"options\":{\"pagination\":{\"page_num\":1,\"page_size\":200},\"projection\":{\"name\":1},\"condition_group\":{\"conjunction\":\"AND\",\"conditions\":[],\"groups\":[]},\"facet\":{\"fields\":[],\"type\":0,\"update_one\":false},\"groups\":[],\"sort\":[]},\"search_types\":[]}","SearchType":4}
当我在邮递员请求中使用以下输出时,它按我的预期工作。但是,当我使用相同的变量通过以下函数发送 http 请求时,出现错误。
url = 'www.myurl.com'
body =request
headers = {
'Token': 'sas'}
r = requests.post(url, data=body, headers=headers)
print(r.text) #prints an error {"statusCode":-1,"errMsg":"unexpected end of JSON input"}
为什么我的输出在 postman 上有效,而在 python 本身中无效?