Trying to call an api with Python where a part of the headers contain "{}".
With Curl it works directly:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'token: {"uid": "[email protected]","timestamp": 0,"token": "","client": "web","version": "","language": "zh-CN" }' -d '{ \
"account": "[email protected]", \
"pwd": "mypassword", \
"is_local": true, \
"agreement_agreement": 0 \
}' 'http://globalapi.sems.com.cn:82/api/v1/Common/CrossLogin'
But with Python I cannot get it to work, the api throws an error. I suspect it is due to the format of the token in the header as it is a string containing {}. See the different variants commented out below - the api accepts none of them. It works fine to use {} in a dict with Python just using regular code:
sems_headers = {
'Content-Type':'application/json',
'Accept':'text/json',
'token': '{"uid": "[email protected]","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}'
}
for c, d in sems_headers.items():
print(c, d)
How can I call the api with the format required of the token in Python?
sems_headers = {
'Content-Type':'application/json',
'Accept':'text/json',
#'token': "'uid':'[email protected]', 'timestamp':'0', 'token':'', 'client':'web', 'version':'', 'language':'en-GB'"
#'token': '{"uid": "[email protected]","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}'
#'token': ''{{"uid": "[email protected]","timestamp": 0,"token": "","client": "web","version": "","language": "en-GB"}}'
#'token': "{""uid"": ""[email protected]"", ""timestamp"": 0, ""token"": "" "", ""client"": ""web"", ""version"": "" "", ""language"": ""en-GB"" }"
}
sems_post_data = {
'account':'[email protected]',
'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)
dictrather than putting it in a string? As in'token': {'uid': '[email protected]', 'timestamp': '0' ...so on}dictastokenis invalid. Header values "must be of type str or bytes, not <class 'dict'>".dict, but please do include the error so we can see what's going wrong