我是新来Python所以请原谅我缺乏良知。。你知道吗
Requirement : Need to process a list of strings through a rest api call
问题: 使用请求,我能够成功地进行api调用并完成工作。使用请求没有问题,只是想研究在每个请求中传递AUTH时使用session是否会加快处理速度。你知道吗
在使用会话时跟踪了多个线索,但继续得到HTTP 400-错误请求。你知道吗
引用:Python Requests and persistent sessions
以下是我正在努力使其工作的代码,任何帮助都将非常有用:
import requests
from requests.auth import HTTPBasicAuth
with open('list.txt') as f:
lines = f.read().splitlines()
s = requests.Session()
r = s.get("https://ucdeploy.domain.com/rest/state", auth=HTTPBasicAuth('username', 'password'))
if r:
print("Logged in Successfully")
else:
print("Login Failed ==> " + str(r))
exit()
for cmp in lines:
print("Processing - " + cmp.strip())
payload = '{"name":"' + cmp.strip() + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
'"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
'"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
'"importAgentType":"inherit","inheritSystemCleanup":"true",' \
'"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
'"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'
response = s.put("https://ucdeploy.domain.com/rest/deploy/component", data=payload)
if response:
print("Successfully Processed")
else:
print("Failed ==> " + str(response))
exit()
输出:
Logged in Successfully
Processing - Component1
Failed ==> <Response [400]>
确定了有效负载格式的问题。你知道吗
由于某些原因,下面的串联代码不起作用:
使用以下内容更新了有效负载连接,它可以工作:
我希望这能帮助别人。你知道吗
相关问题 更多 >
编程相关推荐