Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do you add a new component to a project using the REST API, and python requests library?

Kenny Huang May 31, 2018

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)

2 answers

1 accepted

4 votes
Answer accepted
Arun_Thundyill_Saseendran
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 31, 2018

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(nameprojectdescription=NoneleadUserName=NoneassigneeType=NoneisAssigneeTypeValid=False)

 

Do let me know your result.

 

- Arun

Kenny Huang May 31, 2018

Hi Arun,

Thanks for your help.

 

I found the create_component function in the JIRA-Python library, and found it easier to use.

Arun_Thundyill_Saseendran
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 31, 2018

@Kenny Huang

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

0 votes
Moses Thomas
Community Champion
May 31, 2018

@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

Kenny Huang May 31, 2018

How do I do a POST request using the jira-python library?

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events