简体   繁体   English

使用Python请求的内容类型不正确

[英]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而不是/buildhttps : /buildWithParameters you can specify parameters in the url as query parameters. 您可以在url中指定参数作为查询参数。 you'll love it. 你会爱上它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python请求中单个文件的内容类型 - Content-Type in for individual files in python requests 如何根据服务器使用Python中的请求发送的Content-Type捕获响应? - How to capture the response based on the Content-Type sent by the server using requests in Python? "如何使用 python 请求设置内容类型的边界?" - How can I set boundary of Content-type using python requests? 在某些请求的标头中,内容类型为空白 - Content-type is blank in the headers of some requests 在Python中更改内容类型 - Changing Content-Type in Python 如何在python-requests库中设置POST请求的内容类型? - How do I set the content-type for POST requests in python-requests library? Python请求,如何向multipart/form-data请求添加内容类型 - Python requests, how to add content-type to multipart/form-data request Python 请求不发送 {"Content-Type":"application/json"} 标头或只是忽略 HTTP POST 中的正文 - Python requests not sending {"Content-Type":"application/json"} header or is just ignoring body in HTTP POST python 请求不将我的 POST 数据作为内容类型发送:multipart/form-data auto - python requests not sending my POST data as content-type:multipart/form-data auto 使用python beautiful soup和requests包时,HTML内容不正确 - Incorrect HTML content while using python beautiful soup and requests package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM