"Specify a valid project ID or key" creating issue

Etienne Sillon April 9, 2021

This is the url I'm using: https://xxxx.atlassian.net/rest/api/latest/issue

and this is the payload I got from an example on the net, I only changed the project key:

{"fields":{"summary":"TEST SUMMARY","issuetype":{"name":"Bug"},"project":{"key":"ET"}}}

BTW, I got the project key from /project/search:

{

  "total": 1,

  "isLast": true,

  "maxResults": 50,

  "values": [{

    "avatarUrls": {...},

    "entityId": "...",

    "isPrivate": false,

    "uuid": "...",

    "expand": "description,lead,issueTypes,url,projectKeys,permissions,insight",

    "simplified": true,

    "name": "...",

    "self": "https://....atlassian.net/rest/api/2/project/10000",

    "style": "next-gen",

    "id": "10000",

    "projectTypeKey": "software",

    "key": "ET",

    "properties": {}

  }],

  "self": "https://....atlassian.net/rest/api/latest/project/search?maxResults=50&startAt=0",

  "startAt": 0

}

I also checked I have access to create issues using /mypermissions?permissions=CREATE_ISSUES

"CREATE_ISSUES": {

    "havePermission": true,

    "name": "Create Issues",

    "description": "Ability to create issues.",

    "id": "11",

    "type": "PROJECT",

    "key": "CREATE_ISSUES"

  }

I followed the authentication tutorial here: https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-oauth-authentication/

... and based my code on the examples here: https://bitbucket.org/atlassianlabs/atlassian-oauth-examples/src/master/

Any ideas? Thanks :)

 

2 answers

1 accepted

1 vote
Answer accepted
Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2021

Dear @Etienne Sillon ,

if you try to change the project key of an entire Jira project, I doubt that this is implemented with REST. You need to do this within the UI.

If you want to change the issue key of a single issue, you need:

  • create a new one
  • clone its content
  • link and delete old

This is a "work-a-round". Unfortunately there is no MOVE rest endpoint yet implemented. See JRASERVER-71343 for a suggestion for Server. Its same in Cloud.

So long

Thomas

Etienne Sillon April 10, 2021

Hi Thomas,

Thank you very much for the reply. I'm not trying to change the project key, I'm just trying to create an issue in the project but I don't seem to be able to specify the correct key.

When I do .../project/search I get my project info including "key": "ET", so that's what I use to create the issue with  .../issue with payload: {"fields":{"summary":"TEST SUMMARY","issuetype":{"name":"Bug"},"project":{"key":"ET"}}}

Cheers,

Etienne

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2021

Dear @Etienne Sillon ,

sorry for misunderstanding you.

Your question is tagged as "cloud". In this case you do not specify the project key but it's id, instead.

"project": {"id": "10000"}

See more here.

So long

Thomas

Etienne Sillon April 11, 2021

Hi Thomas, 

Thanks again for the reply. I had tried the project id as well but got the same result. 

I did manage to create an issue by creating an API token and using the curl command as per the link you sent:

curl --request POST \
--url 'https://xxx.atlassian.net/rest/api/3/issue' \
--user 'xxxxx:xxxxx' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"update": {},
"fields": {
"summary":"TEST SUMMARY","issuetype":{"id":"10001"},"project":{"id":"10000"}
}
}'

Not sure why the exact same payload doesn't work through the Java client, even thought I can access the project and issues using it.

Anyway, all good, I'll use the API token version, thanks again!

Etienne

Raffaele Messina July 14, 2021

@Etienne Sillon hey i am having the same problem; via postman client everything is fine, but in java client i can't get it to work. Did you solve the problem in the end?

Etienne Sillon July 14, 2021

@Raffaele Messina yep, this is the code I used:

/******************************************************************************************************/

    

    public static void createIssue(String projectId, String issueTypeId, String summary, String description) {

 

    String url = "https://xxxxx.atlassian.net/rest/api/latest/issue";

    JSONObject payload = getCreateIssuePayload(projectId, issueTypeId, summary, description);

        sendHttpPost(url, payload);

 

    }

    

/******************************************************************************************************/

    

    private static JSONObject getCreateIssuePayload(String projectId, String issueTypeId, String summary, String description) {

 

    JSONObject ret = new JSONObject();

 

        try {

 

        JSONObject fieldsData = new JSONObject();

 

        JSONObject projectData = new JSONObject();

            projectData.put("id", projectId);

    fieldsData.put("project", projectData);

       

    fieldsData.put("summary", summary);

    fieldsData.put("description", description);

 

    JSONObject issueTypeData = new JSONObject();

        issueTypeData.put("id", issueTypeId);

    fieldsData.put("issuetype", issueTypeData);

       

        ret.put("fields", fieldsData);

       

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        

        System.out.println(ret.toString());

    

return ret;

 

}

 

/******************************************************************************************************/

    

public static String sendHttpPost(String url, JSONObject payload) {

 

    System.out.println("sendHttpPost(): url=" + url);

 

String ret = "";

 

 

    HttpClient httpclient = HttpClients.createDefault();

    

    HttpPost post = new HttpPost(url);

    post.addHeader("Accept" , "application/json");

    post.addHeader("Content-Type" , "application/json");

    post.addHeader("Authorization" , "Basic XXXXXXXXX");

 

    try {

    

    StringEntity payloadEntity = new StringEntity(payload.toString());

    post.setEntity(payloadEntity);

   

    HttpResponse response = httpclient.execute(post);

 

        int code = response.getStatusLine().getStatusCode();

        System.out.println("status=" + code);

 

    HttpEntity entity = response.getEntity();

 

    if (entity != null) {

        InputStream instream = entity.getContent();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(instream), 1);

        try {

                String line;

                while ((line = bufferedReader.readLine()) != null) {

                ret += line;

                }

                instream.close();

                bufferedReader.close();        

            } finally {

            instream.close();

                bufferedReader.close();        

        }

    }

} catch (Exception e) {

e.printStackTrace();

}

    

    if(ret.length() == 0) {

    ret = "I'm sorry, there was an error processing your request";

    }

 

    System.out.println("sendHttpPost(): ret=" + ret);

    

    return ret;

    

}

Richard Chen September 3, 2021

Getting the exact same issue here. Our organization needs this to automate the creation of bugs.

2 votes
Raphael Arias January 22, 2022

I fixed mine by wrapping the whole payload under `fields`.

Kyle Wright August 2, 2022

This was the same problem I had.

Anil.Ashokupadhyaya March 26, 2023

Could any share the  payload getting the same 400 response code

This is my below payload JSON

 

payload = json.dumps(
{
"fields" : {
"project": {
"id":"10000"
},
"summary": "Create JIRA issue",
"issuetype": {
"id": "10001"
},
"parent": {
"key": "PM"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "This is the description.",
"type": "text"
}
]
}
]
}
}
})

Suggest an answer

Log in or Sign up to answer