Reputation: 658
I have following code which creates subtask in JIRA
inf = open('/var/lib/rundeck/output.txt')
for line in inf:
print line
headers = {'Content-Type': 'application/json',}
data = '{"fields":{"project":{"key":"TECH"},"parent":{"key":line},"summary":"Create AD List ","description":"","issuetype":{"name":"Sub-task"},"customfield_10107":{"id":"10400"}}}'
response = requests.post('https://jira.company.com/rest/api/latest/issue/', headers=headers, data=data, auth=('user', 'pass'))
inf.close()
i have a file (output.txt), python for every line found (TECH-XXX) printss all lines, it should trigger script above.
when i hard-code key "key":"TECH-1147"
instead of "key":line
script generates subtask, but when substituting variable (line), nothing happens
Ouptut.txt:
TECH-1234
TECH-1345
.........
i convereted this code:
curl -D- -u: user:Pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"Create AD List of all Active Users\",\"description\":\"some description\",\"issuetype\":{\"name\":\"Sub-task\"},\"customfield_10107\":{\"id\":\"10400\"}}}" -H "Content-Type:application/json" https://company.com/rest/api/latest/issue/
using this https://curl.trillworks.com/
tried also {"key":'"' + line + '"'}
and getting {u'errorMessages': [u'The issue no longer exists.'], u'errors': {}}
Issue is TECH-1247 (variable) which definitely exists
Upvotes: 0
Views: 185
Reputation: 52858
Maybe try using rstrip()
to trip any trailing whitespace/newlines and json.dumps()
so the data isn't passed as form-encoded...
import requests
import json
with open("output.txt", "rb") as infile:
for line in infile:
headers = {"Content-Type": "application/json"}
data = {"fields": {
"project": {"key": "TECH"},
"parent": {"key": line.rstrip()},
"summary": "Create AD List ",
"description": "",
"issuetype": {"name": "Sub-task"},
"customfield_10107": {"id": "10400"}
}}
response = requests.post("https://jira.company.com/rest/api/latest/issue/",
headers=headers,
data=json.dumps(data),
auth=("user", "pass"))
Like another answer said, if you use json
param instead of the data
param, the dict will automatically be encoded for you and the Content-Type set to application/json.
See here for more information.
Upvotes: 3
Reputation: 28762
line
is not interpreted as a variable. It's just a string. One solution is to use the %
operator for string formatting:
inf = open('/var/lib/rundeck/output.txt')
for line in inf:
print line
headers = {'Content-Type': 'application/json',}
data = '{"fields":{"project":{"key":"TECH"},"parent":{"key":%s},"summary":"Create AD List ","description":"","issuetype":{"name":"Sub-task"},"customfield_10107":{"id":"10400"}}}' % line
response = requests.post('https://jira.corp.hentsu.com/rest/api/latest/issue/', headers=headers, data=data, auth=('user', 'pass'))
inf.close()
Note that line
was replaced with %s
and then % line
was added to the end. This will replace the %s
with the value of the variable line
.
Upvotes: 1