[英]Content-Type incorrect using Python Requests
I am attempting to kick off a paramaterized Jenkins job using the Python Requests library (2.17.3 and Python 3.6) but I am running into issues. 我正在尝试使用Python Requests库(2.17.3和Python 3.6)来启动参数化的Jenkins作业,但是遇到了问题。
I have tested with Curl and it works and kicks off the Jenkins job like I would expect. 我已经对Curl进行了测试,它可以正常工作,并且像我期望的那样开始了詹金斯的工作。 The following is a simplified version of the curl command I used.
以下是我使用的curl命令的简化版本。
curl -X POST -H "$crumb" $jenkins_url/job/$job_name/build?token=$job_token \
--user $auth \
--data-urlencode json='
{"parameter":
[
{"name":"parameter1", "value":"test1"},
{"name":"parameter2", "value":"test2"},
{"name":"git_repo", "value":"'$my_repo'"},
{"name":"git_tag", "value":"'$git_tag'"}
]
}'
Doing the same thing with Python though is causing problems. 尽管用Python做同样的事情会引起问题。 Here is (basically) what I'm doing in Python.
(基本上)这是我在Python中所做的事情。
import requests
job_url = JENKINS_URL + "/job/" + JOB_NAME + "/build?token=" + JOB_TOKEN
params = {
"parameter":
[
{"name":"parameter1", "value":"test1"},
{"name":"parameter2", "value":"test2"},
{"name":"git_repo", "value":"'$my_repo'"},
{"name":"git_tag", "value":"'$git_tag'"}
]
}
job = requests.post(job_url, headers=headers, auth=(JENKINS_USER, JENKINS_PASS), data=params)
And when I examine the response, I get Error 400 This page expects a form submission
in the returned job.content
. 当我检查响应时,出现
Error 400 This page expects a form submission
在返回的job.content
Error 400 This page expects a form submission
job.content
。 Looking at the content-type in the job object I see that it is set to text/html
which doesn't seem right, but I don't know why it doesn't respect the header that is supposed to get set. 查看作业对象中的内容类型,我发现它设置为
text/html
,这似乎不正确,但是我不知道为什么它不遵守应该设置的标头。
print(job.headers)
{'Server': 'nginx/1.10.0 (Ubuntu)', 'Date': 'Sat, 10 Jun 2017 01:57:04 GMT', 'Content-Type': 'text/html;charset=iso-8859-1', 'Content-Length': '392', 'Connection': 'keep-alive', 'X-Content-Type-Options': 'nosniff', 'Set-Cookie': 'JSESSIONIDXXX;Path=/;Secure;HttpOnly', 'Cache-Control': 'must-revalidate,no-cache,no-store'}
I have tried setting the Content-Type
header manually in the code but it doesn't seem to make a difference. 我尝试在代码中手动设置
Content-Type
标头,但似乎没有什么不同。
headers['Content-Type'] = 'application/x-www-form-urlencoded'
Is there something I am doing wrong or missing? 我做错了什么或想念什么吗?
use /buildWithParameters
instead of /build
like in this answer: https://stackoverflow.com/a/35913411/6090676 . 在此答案中使用
/buildWithParameters
而不是/build
: https : /buildWithParameters
。 you can specify parameters in the url as query parameters. 您可以在url中指定参数作为查询参数。 you'll love it.
你会爱上它。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.