我对Python完全陌生。我需要发布一个认证请求到一个网站使用标题和身体细节。到目前为止,我已经发布了下面的代码。当我运行它时,我会得到这一行的语法错误:
req.add_header('API-KEY': 'my_api_key', 'ACCOUNT-ID': 'my_account_id', 'Content-Type': 'application/json; charset=UTF-8', 'VERSION': '2')请你回顾一下,让我知道我哪里出了问题?
import urllib.request
import json
body = {'identifier': 'my_id', 'password': 'my_encrypted_pwd', 'encryptedPassword': true}
url = 'https://mywebsite.com/gateway/deal/session'
req = urllib.request.Request(url)
req.add_header('API-KEY': 'my_api_key', 'ACCOUNT-ID': 'my_account_id', 'Content-Type': 'application/json; charset=UTF-8', 'VERSION': '2')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('UTF-8')
req.add_header('Content-Length', len(jsondataasbytes))
print (jsondataasbytes)
response = urllib.request.urlopen(req, jsondataasbytes)发布于 2017-09-17 06:58:21
我建议您使用requests模块,因为它比urllib.request更快更好
response = requests.put(
url,
body, headers={'API-KEY': 'my_api_key',
'ACCOUNT-ID': 'my_account_id',
'Content-Type': 'application/json; charset=UTF-8',
'VERSION': '2'
}
)现在,您可以像往常一样解析得到的response。
https://stackoverflow.com/questions/46261478
复制相似问题