我已使用request.post成功连接到目标服务器;但是,当我尝试进行任何request.get调用时,出现以下错误:
{"message":"Not logged in","code":401}
我已确认我已成功登录,因为当我打印(response.text)时,我正在显示来自服务器的信息。根据错误消息,看来我没有保留登录名。
我的脚本:
import requests
data = {}
payload = {'j_username': 'my_username', 'j_password': 'my_password'}
response = requests.post('https://sample.com', data=payload)
getBlocks_response = requests.get('https://sample.com/api/v1/resourceBlock')
print(getBlocks_response.text)
getDataDateRange_response = requests.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload)
print(getDataDateRange_response.text)
我期望以JSON形式输出数据,但是上面的两个request.get调用都只收到两个错误。我是Python和API交互的新手。任何见识将不胜感激。谢谢。
答案 0 :(得分:0)
我阅读了有关Session()的文档。通过将脚本调整为:
,我能够使我的get请求成功运行。import requests
with requests.Session() as session:
data = {}
payload = {'j_username': 'my_username', 'j_password': 'my_password'}
response = session.post('https://sample.com', data=payload)
getBlocks_response = session.get('https://sample.com/api/v1/resourceBlock')
print(getBlocks_response.text)
getDataDateRange_response = session.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload)
print(getDataDateRange_response.text)