Hi, I am trying to add a new component to an existing project using the REST API and the requests python library. When I tried running the below code, I get a 400 error that says
"{"errorMessages":["Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN\n at [Source: org.apache.catalina.connector.CoyoteInputStream@354be5ec; line: 1, column: 6]"]}"
Can someone give me some ideas as to what the mistake is? Thanks
auth = requests.auth.HTTPBasicAuth('user', 'password')
url = jira url
params = {
"name": "Test",
"description": "this is a component",
"leadUserName": "mhuh",
"assigneeType": "UNASSIGNED",
"isAssigneeTypeValid": False,
"project": "projectname" }
rest_url = url + '/rest/api/2/component'
headers ={'Content-Type': 'application/json'}
print rest_url
response = requests.post(rest_url, data = params, auth=auth, headers=headers, verify=False)
Hi @Kenny Huang
The problem is that you are passing the payload as params and hence it is not able to process your request.
You can use the below code, which a modified version of your code.
# Python 2.7
import json
import requests
auth = requests.auth.HTTPBasicAuth('user', 'password')
url = 'http://jira_url.com'
params = {
"name": "Test",
"description": "this is a component",
"leadUserName": "mhuh",
"assigneeType": "UNASSIGNED",
"isAssigneeTypeValid": False,
"project": "projectname"
}
rest_url = url + '/rest/api/2/component'
headers ={'Content-Type': 'application/json'}
print 'ReST URL Created = ' + rest_url
# From here is the real change
# 1. Convert Your Params Dict to a JSON
payload = json.dumps(params)
# 2. Use the JSON to post
response = requests.post(rest_url, data=payload, auth=auth, headers=headers, verify=False)
Alternatively you can make of the JIRA-Python library.
https://jira.readthedocs.io/en/master/api.html?highlight=component#jira.JIRA.create_component
If you are creating a component using the JIRA-python library, then there is no need for you to do an explicit post. You can make use of the create_component() method in the library and the method will take care of calling the ReST API with the required Verb.
create_component
(name, project, description=None, leadUserName=None, assigneeType=None, isAssigneeTypeValid=False)
Do let me know your result.
- Arun
Hi Arun,
Thanks for your help.
I found the create_component function in the JIRA-Python library, and found it easier to use.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great!
If you think the solution can be used other who stumble on this problem as well, please mark the above answer as accepted.
- Arun
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Kenny HuangTry this and see if you will pass the authentication
from jira import JIRA
jira = JIRA(basic_auth=('user', 'pass'), options={'server':'https://xacount.atlassian.net'})
params = {
............
..... }
......
Best
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.