Rest API POST to create an issue, getting 400 bad request (Using Salesforce APEX)

Mike Schumacher August 11, 2022

I was able to get the Rest API to GET issues working and get a response of all issues.  I am trying to do a POST and create an issue and keep getting 400 bad request.  I can't see an error in the json string for the body.

 

Here is the Salesforce Apex code:

// trying  POST to create issue
stringData = '{"update": {}, "fields" : {"project" : {"key" : "IMWW"},"issueType":{"name":"Task"}, "summary" : "Rest test from SF", "description" : "new description from SF to Jira API"}}';
http = new Http();
request = new HttpRequest();
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(pwBase64) );
request.setEndpoint('https://<mydomain>.atlassian.net/rest/api/latest/issue');
request.setBody(stringData);
request.setMethod('POST');
response = http.send(request);

 

2 answers

1 accepted

0 votes
Answer accepted
Mike Schumacher August 13, 2022

I added a Sytem.bebug('responseBody=' + response.getBody() ); to my code to get a little more detail.

Despite seeing examples for the json for the issueType as:

"issueType":{"name":"Task"}

 

This was the incorrect syntax.   Two things I had to correct.  issueType must be all lower case as "issuetype" and had to use "id" : <actual issuetype id value>

 

The correct json format for my test create issue should look like:

 

stringData = '{ "fields" : {"project" : {"key" : "IMWW"},"issuetype":{"id":"10004"}, "summary" : "Rest test from SF", "reporter":{"id":"5f3ecef6fcaf93003bc95ca6"} }}';

 

The issuetype keyword must be lowercase.  I missed this initially.   You can provide the issuetype "id" or the "name".  Once i corrected and used "issuetype", I could use {"name":"Task"} too.

 

Also need to include the "reporter" "id" as well.

 

Hope this helps someone else.

 

-Mike

Marco Vanali January 26, 2023

Hi @Mike Schumacher ! I am having your same issue. The only blockers I have is.. how di I get my reporter id?

Is there an easy way through the website?

Thanks

0 votes
GA
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 25, 2024

From my experience now (2024), the only fields that are required are project, issue type, and summary. Here is a working Javascript example extended from @Mike Schumacher's helpful answer:

const body = {
"fields": {
"project": {
"key": "ssss",
},
"issuetype": {
"name": "Task"
},
"summary": "Rest test from SF"
}
}

try {
const response = await fetch(..., {
method: 'POST',
headers: {
},
body: JSON.stringify(body)
}

...
} catch ...

"ssss" is the name of the project. And the issuetype name MUST match an existing issue type. This was the issue that burned me for a while. I was using "Service request" - which kept failing, until I finally realized that in my case, it needed to be "[System] Service request".

Using the "Task" issue type, thanks to the example above, is what led me to the realization that my 400 error was caused by an inaccurate issue type. And I suspect "Task" is a valid issue type across all Jira instances.

Lastly, while it's not required, if you do want to include the reporter id, you can find it as follows:

  • Click on your profile image (circle) in the top right of your Jira instance
  • Select Profile
  • The number in the url address bar, after the term "people", is your Jira id: e.g. jirainstance.atlassian.net/jira/people/nnnnnnnnnnnnn (the n's are your id)

I hope this helps.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events