Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

#!/usr/bin/python import requests import json import sys url =…

  1. 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)) 这个程序有什么问题

  2. Assistant

    这个程序存在以下几个问题:

    1. 请求头部 sso.jd.com 错误,应该是 Host 字段,不需要添加前缀 http://
    2. 请求的 Content-Type 应该是 application/json;charset=utf-8,而不是 application/json
    3. data 参数应该是经过 json.dumps 方法处理后的字符串,而不是传入列表对象
    4. 在执行请求之前,应该先将 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))