Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在请求头中使用JSON_Python_Python Requests - Fatal编程技术网

Python 在请求头中使用JSON

Python 在请求头中使用JSON,python,python-requests,Python,Python Requests,试图用Python调用一个api,其中一部分头包含“{}” 使用Curl,它可以直接工作: curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'token: {"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language":

试图用Python调用一个api,其中一部分头包含“{}”

使用Curl,它可以直接工作:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'token: {"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "zh-CN" }' -d '{ \ 
   "account": "email@domain.com", \ 
   "pwd": "mypassword", \ 
   "is_local": true, \ 
   "agreement_agreement": 0 \ 
 }' 'http://globalapi.sems.com.cn:82/api/v1/Common/CrossLogin'
但是对于Python,我无法让它工作,api抛出了一个错误。我怀疑这是由于标头中标记的格式,因为它是包含{}的字符串。 请参见下面注释的不同变体-api不接受任何变体。 在dict中使用{}与Python配合使用,只需使用常规代码即可:

sems_headers = {
    'Content-Type':'application/json',
    'Accept':'text/json',
    'token': '{"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}' 
}
for c, d in sems_headers.items():
    print(c, d)
如何使用Python中令牌所需的格式调用api?

sems_headers = {
    'Content-Type':'application/json',
    'Accept':'text/json',
    #'token': "'uid':'email@domain.com', 'timestamp':'0', 'token':'', 'client':'web', 'version':'', 'language':'en-GB'" 
    #'token': '{"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}' 
    #'token': ''{{"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}}' 
    #'token': "{""uid"": ""email@domain.com"", ""timestamp"": 0, ""token"": "" "", ""client"": ""web"", ""version"": "" "", ""language"": ""en-GB"" }"
}

sems_post_data = {
    'account':'email@domain.com',
    'pwd':'mypassword',
    'is_local':True,
    'agreement_agreement':0
}

post = requests.post("https://globalapi.sems.com.cn/api/v1/Common/CrossLogin", headers=sems_headers, data=sems_post_data)

print(post.text)
从requests文档来看,您的问题实际上可能是如何发送JSON负载,因为data关键字参数发送的是表单编码的数据,而不是JSON编码的数据。请尝试显式使用JSON编码负载,或者改用JSON关键字参数:

post=请求。post(“https://globalapi.sems.com.cn/api/v1/Common/CrossLogin“,headers=sems\u headers,json=sems\u post\u data)

此外,您的
Accept
标题应更改为
application/json
,为了解析json响应内容,您应该使用
post.json()
而不是
post.text

总之,这将是:

sems_headers = {
    'Content-Type':'application/json',
    'Accept':'application/json',
    'token': '{"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}' 
}

sems_post_data = {
    'account':'email@domain.com',
    'pwd':'mypassword',
    'is_local':True,
    'agreement_agreement':0
}

post = requests.post("https://globalapi.sems.com.cn/api/v1/Common/CrossLogin", headers=sems_headers, json=sems_post_data)

print(post.json())

您是否尝试过使用实际的
dict
而不是将其放入字符串中?就像在
中的'token':{'uid':'email@domain.com“,”timestamp“:”0“…依此类推}
您还可以包含一份您遇到的特定错误的副本/粘贴,以便我们能够最好地帮助您吗?@awarrier99使用
dict
作为
令牌
无效。标题值“必须是str或bytes类型,而不是”。@MisterMiyagi-yup刚刚在文档中发现了这一点@herrbert忽略了我最初的建议,即使用实际的
dict
,但请务必包含错误,以便我们可以看到发生了什么wrong@MisterMiyagi这是正确的。下面可以参考-谢谢,为请求中的数据添加json转储解决了它@赫伯特:太棒了,没问题
sems_headers = {
    'Content-Type':'application/json',
    'Accept':'application/json',
    'token': '{"uid": "email@domain.com","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}' 
}

sems_post_data = {
    'account':'email@domain.com',
    'pwd':'mypassword',
    'is_local':True,
    'agreement_agreement':0
}

post = requests.post("https://globalapi.sems.com.cn/api/v1/Common/CrossLogin", headers=sems_headers, json=sems_post_data)

print(post.json())