gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
#!/usr/bin/python import requests import json import sys url =…
- User
#!/usr/bin/python import requests import json import sys
url = "http://hrsearch.jd.com/search/byKey"
payload = [{"searchKey":"zongyue1","page":1,"pageSize":20,"searchIndex":"ehr-person*"}] headers = { 'sso.jd.com': 'BJ.14E22D2CF49C82855B42C7EB6022DEDC.4120230905094533', 'Content-Type': 'application/json' }
response = requests.request("POST", url, headers=headers, data=payload)
parsed = json.loads(response.text) print(json.dumps(parsed,indent=4,ensure_ascii=False)) 这个程序有什么问题
- Assistant
这个程序存在以下几个问题:
- 请求头部
sso.jd.com错误,应该是Host字段,不需要添加前缀http:// - 请求的
Content-Type应该是application/json;charset=utf-8,而不是application/json data参数应该是经过json.dumps方法处理后的字符串,而不是传入列表对象- 在执行请求之前,应该先将
payload参数转换为字符串格式
修正后的代码如下:
import requests import json url = "http://hrsearch.jd.com/search/byKey" headers = { 'Host': 'sso.jd.com', 'Content-Type': 'application/json;charset=utf-8' } payload = json.dumps([{"searchKey":"zongyue1","page":1,"pageSize":20,"searchIndex":"ehr-person*"}]) response = requests.post(url, headers=headers, data=payload) parsed = json.loads(response.text) print(json.dumps(parsed, indent=4, ensure_ascii=False)) - 请求头部