使用请求在Python中更新Jira中的字段

时间:2017-12-20 16:58:17

标签: python python-requests jira

我正在尝试使用以下代码更新Jira中的自定义字段,并且我收到了415错误代码。任何帮助将不胜感激!我的代码如下:

import json
import requests

jiraSession = requests.Session()


username =  input("username: ") 
password =  input("password: ")   

#-------------- CALL -----------------

searchAPI = 'http://ourbaseurl.jira.com/rest/api/2/issue/ISSUE-1'
holder = requests.put(searchAPI, auth=(username, password), data=
    {"update":
        {"customfield_1000":
            {
                "set":
                    {
                        "value": "FIELD VALUE"
                    }
            }
        }})

修改

我已更新了我的代码,现在我获得了400状态代码,基于/ rest / api / 2 / issue / ISSUE-NAME / editmeta

data = json.dumps({"fields": { "customfield_SOMEFIELD": {"value": "SOME DATA"}}})
holder = requests.put(searchAPI, json=(data) , auth=(username, password))

1 个答案:

答案 0 :(得分:0)

标头中似乎缺少标头,请尝试包含标头信息,我可以使用python请求标头代码来更新字段,如下所示

import json
import requests
from requests.auth import HTTPBasicAuth
import getpass

user=input("enter your username - ")
password= getpass.getpass("enter your password - ")

read_text = open("jira_numbers.txt",'r').read().split('\n')

for jira_num in read_text:
    url="blah_blah_blah"+jira_num
    header = {'Content-Type':'application/json'}
    jiraF=open("jira_update.json","r")
    jira_load = jiraF.read()
    r = requests.put(url, data = jira_load, headers = header, auth = HTTPBasicAuth(user,password))
    print("the json response \n", r.status_code, r.headers )
    print("the response content is \n", r.content)

jira_update.json文件的内容为:

{
    "fields": {
                "customfield_13444":"blah_blah"
              }
}