簡體   English   中英

使用Python請求的內容類型不正確

[英]Content-Type incorrect using Python Requests

我正在嘗試使用Python Requests庫(2.17.3和Python 3.6)來啟動參數化的Jenkins作業,但是遇到了問題。

我已經對Curl進行了測試,它可以正常工作,並且像我期望的那樣開始了詹金斯的工作。 以下是我使用的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'"}
    ]
  }'

盡管用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)

當我檢查響應時,出現Error 400 This page expects a form submission在返回的job.content Error 400 This page expects a form submission job.content 查看作業對象中的內容類型,我發現它設置為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'}

我嘗試在代碼中手動設置Content-Type標頭,但似乎沒有什么不同。

headers['Content-Type'] = 'application/x-www-form-urlencoded'

我做錯了什么或想念什么嗎?

在此答案中使用/buildWithParameters而不是/buildhttps : /buildWithParameters 您可以在url中指定參數作為查詢參數。 你會愛上它。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM