Forums

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

Unable to create Issue via REST API

naveen.tak
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!
June 10, 2019

Hi

I've seen multiple threads and did go through JIRA REST tutorial.

1. Generated the Access token to use it for Basic Auth. 

Here is the Complete Request:

POST /rest/api/2/issue/ HTTP/1.1
Host: libertydhub.atlassian.net
Content-Type: application/json
Authorization: Basic **************
User-Agent: PostmanRuntime/7.13.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 7f778032-b7f7-4d27-80f8-89b522fd72cd,1b0490eb-28d5-4066-8f49-76000b1cebbf
Host: {hostname}
cookie: atlassian.xsrf.token=B2M1-T7HW-8IGG-KJSU_09cc563dfe0ce7f0af5eb9c19b8df3fa4ec60a35_lout
accept-encoding: gzip, deflate
content-length: 278
Connection: keep-alive
cache-control: no-cache

{
"fields": {
"project": {
"key": "LP"
},
"issuetype": {
"name": "Story"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names u"
}
}

 

Error response

 

{
"errorMessages": [],
"errors": {
"summary": "Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.",
"description": "Field 'description' cannot be set. It is not on the appropriate screen, or unknown."
}
}

2 answers

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 14, 2019

Hi Naveen,

I see that you are running into errors when trying to create an issue using the Jira REST API.   That specific error can be very vague. There are a couple of different possible causes for this.

  1. The screens for this issue type do not contain those fields
  2. Your account does not actually have permissions to create new issues
  3. Your REST call is not properly authenticating your credentials, which can lead you to cause #2 again

Could you please let me know what specific tutorial you are attempting to follow?  It could be we have some out dated documentation and I'd like to update that if that is the case here. 

I see you mentioned creating an API Token.  However depending on how you make this request will determine how to use that token. Check out Basic auth for REST APIs for the two different examples. If you were using curl you could use the -u parameters to pass a emailaddress:API_TOKEN as a string to authenticate.  But I suspect that you're not doing that, but rather following the second example where you can supply the auth in a header.  When you do that, you have to encode your username:API_TOKEN into a base64 string first and use that.  You can't just use the API Token directly there.

This could explain the error message you have seen here.

Try that first.  It might also help to start by first making a GET call via the REST API to see if you can return issue data, say GET /rest/api/2/issue/{issueIdOrKey}.  As long as the issuekey you try to retrieve is not publicly accessible anonymously, you will still have to authenticate successfully with an account that can view that issue to get back any data.   It can be a good test to make sure you are authenticated correctly here first.  If that works, then try to make a POST call to see if you can create/save some data.

I hope this helps, please let me know.

 

Andy

Haim Schlesinger
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!
December 22, 2020

Thanks you! this was very helpful

0 votes
Shubham July 13, 2022

Hey guys

I am using exactly same java code for creating jira ticket using username:password and username:api_token.

I am able to create ticket using username:password (ofcourse encoded with base64) but when I am using the same code for api token , it is giving me 401 unauthorized - no body error with restTemplate.

 

Please suggest me the solution for this.

 

Thanks

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 13, 2022

The use of the API token is still valid for this.  But many times users are confused over which API token to use.  For requests like this, use the API token for your account which you can create by following the steps in Manage API tokens for your Atlassian account.

I've recently found other Cloud site-admins and Organization admin users are getting confused because they can create an API token for the Admin API as explained in Manage an organization with the admin APIs.  These API tokens are not interchangeable, and these will not work with the product REST APIs.

 

If you are sure you are using the correct token type, then please follow the steps in Basic auth for REST APIs.  I have encountered users that have used other means to encode the string and found in some cases, it can add unexpected characters, hence leading to a failed authorization.

Shubham July 13, 2022

Hey   @Andy Heinzer
I followed the same step which you mentioned and the api token is working fine with postman.

 

Let me share some snippet with you .

Yeah I have created token from my account profile>Personal Access token  and using the same while authorization.

I am using something like this.. with proper url,username and api token.

 

public HttpHeaders createHeadersWithAuthentication()
{
String plainCreds = "myusername" + ":" + "api_token";
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
String base64Creds = new String(base64CredsBytes);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Authorization", "Basic " + base64Creds);
return requestHeaders;
}

httpHeaders = createHeadersWithAuthentication();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String url = "https://onejira.******.com/rest/api/2/issue";
HttpEntity<String> requestEntity = new HttpEntity<String>(jiraRequest.toString(), httpHeaders);
String jiraResponse = restTemplate.postForObject(url ,requestEntity, String.class);

 

When I am replacing 

String plainCreds = "myusername" + ":" + "api_token";

with 

String plainCreds = "myusername" + ":" + "mypassword";

It is working perfectly fine for me with the same lines of code.. even with postman i tried using token instead of password , both are working fine , only issue is with using token inside code.


 

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 13, 2022

Oh, you're using Jira Server or Jira Data Center.  Those products use personal access tokens.  The concept is similar to how our Atlassian Cloud uses REST API tokens, but the previous documentation I cited was specific to our Cloud platform.

The authorization headers are completely different when using Server/DC Personal access tokens.  From that document I linked to this reply:

Using PATs

To use a personal access token for authentication, you have to pass it as a bearer token in the Authorization header of a REST API call.

Here's an example using cURL to call the REST API with a bearer token:

curl -H "Authorization: Bearer <yourToken>" https://{confluenceBaseUrl}/rest/api/content

Note that the Authorization is using just a "Bearer" term instead of Basic that is used in Cloud.  Also note that you just supply the token in this case.  There is no encoding of credentials for Personal Access Tokens, whereas in our Cloud platform, you typically have to encode an emailaddress:apitoken to generate the authorization header.

Andy

Shubham July 23, 2022

Hi @Andy Heinzer 

 

Thanks for your help. The process which you explained worked for me !  but while fetching the details of an issue, I am facing problem.

String url = "https://onejira.******.com/rest/api/2/issue+ "/" + jiraId;
httpHeaders = createHeadersWithAuthentication();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(httpHeaders);
String jiraResponse = restTemplate.getForObject(url, String.class);

private HttpHeaders createHeadersWithAuthentication() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Authorization", "Bearer " + "myApiKey");
return requestHeaders;
}
}

 

I am getting error 

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{"errorMessages":["You do not have the permission to see the specified issue.","Login Required"],"errors":{}}"

 

What else can be done here.. Please help

 

Thanks

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 25, 2022

With that error message it means one of two things must be true.  Either your credentials have not been entered correctly in the API call OR your account does not have permissions needed within Jira to view that issue.

If you're sure that you have created the personal access token correctly and are using it as described in Personal Access Tokens, then I think the next steps would be to check within Jira itself to make sure your account has access to Browse to the issue in question.  I'd recommend asking a Jira Admin to use the Permission Helper to check that your account can see that issue.

Suggest an answer

Log in or Sign up to answer