creating issue via jira rest api postman

Ankit kharola
Contributor
December 8, 2024

I am trying to create a issue via jira rest api using postman  

 

 

{
    "fields": {
       "project":
       {
          "key": "SOTF"
       },
       "summary": "Issue was created visa postman",
       
       "issuetype": {
          "name": "Bug"
       },
       "description": {
      "content": [
        {
          "content": [
            {
              "text": "Issue created via postman.",
              "type": "text"
            }
          ],
          "type": "paragraph"
        }
      ],
      "type": "doc",
      "version": 1
    }
   
       
    }
}
this is the response i am getting 

 

{
    "errorMessages": [],
    "errors": {
        "project": "Specify a valid project ID or key"
    }
}  the project key is correct as i can view the json response if i use it in a url in the web

1 answer

0 votes
Tomislav Tobijas _Koios_
Atlassian Partner
December 9, 2024

Hi @Ankit kharola ,

You might want to check the following discussions:

Maybe try using GET method to get some data from the existing project to see if authorization works as expected. Once you confirm that, try moving to create a new issue.

Also, here's an official REST API docs on how to create a new issue using POST method: REST API docs / Issues / Create issue 

Cheers,
Tom

Ankit kharola
Contributor
December 9, 2024

Hi thank you for your response i wanted to check if we can create issues from one instance to another  via jira rest api i found few articles where we could do that by automation

Tomislav Tobijas _Koios_
Atlassian Partner
December 9, 2024

Yeah, that's possible as well, although you'll have to use web requests within automation to get that done.

I've written one article on that topic, however, it's in Croatian (you can translate it). It explains how you can create 'sync' between two instances with automation: https://atlassian.koios.hr/sync-issuea-dviju-jira-cloud-instanci/

Maybe this can help as well.

Cheers,
Tom

Ankit kharola
Contributor
December 9, 2024

Thank you so much sir

Ankit kharola
Contributor
December 9, 2024

Hi @Tomislav Tobijas _Koios_  i was going through the websites where it said we could do that using automation by creating rules in jira. i was wondering can i do the same with code? i have created a jira global page module in my instance and it just has a simple button wwhich says create issue and when user clicks on it i want to create issue in another instance which i am a part of and have the permision to create issues

Tomislav Tobijas _Koios_
Atlassian Partner
December 9, 2024

Hi @Ankit kharola ,

...i have created a jira global page module in my instance...

I have never created something like this. Did you use ScriptRunner or something else to create a 'site button'?

But yes - generally it's probably possible. You just need to use REST API/send the call to another instance which will then create an issue within a specified project, with defined information such as issue type, summary, etc.

Ankit kharola
Contributor
December 9, 2024

Hi thank you for the response i am using this api in a forge app i have added the url of instance where i want to create the  in my manifest file 

resolver.define('createIssue', async () => {
//   try {
//     const bodyData = {
//       fields: {
//         project: {
//           key: 'SOTF',  // Project key in the target Jira instance
//         },
//         summary: 'Issue has been created',  // Issue summary
//         issuetype: {
//           name: 'Task',
//         },
//         description: 'This issue is related to a Forge integration',  // Issue description (plain text)
//       },
//     };

//     // Define the Jira instance URL
//     const targetJiraUrl = 'https://learning-forge.atlassian.net';  // Replace with the target Jira instance URL

//     // Set up authentication for the target instance (Example: using Basic Authentication)
//     const authHeader = {
//       'Authorization': 'Basic ' + Buffer.from('ankit.kharola@sparxsys.com' + ':' + 'my-api-key').toString('base64'),
//     };

//     // Making a POST request to create an issue in the target Jira instance
//     const response = await fetch(`${targetJiraUrl}/rest/api/2/issue`, {  // Use `/rest/api/2/issue` for Jira API v2
//       method: 'POST',
//       headers: {
//         'Accept': 'application/json',
//         'Content-Type': 'application/json',
//         ...authHeader,  // Include the authorization header
//       },
//       body: JSON.stringify(bodyData),  // Convert the bodyData object to JSON string
//     });
//   console.log(response, "res")
//   if(response.ok === true){
//     console.log("response is okay")
//     const issue = await response.json();  // Read the response as JSON
//        console.log('Created issue:', issue);  // Log the created issue data
//       return issue;  // Return the created issue data
//   }
//     // Handle the response: explicitly consume the response body
//     if (!response.ok) {
//       const errorBody = await response.text();  // Use `.text()` to get the raw error message
//       console.error('Error response body:', errorBody);  // Log the error body
//       throw new Error(`Failed to create issue in target Jira instance: ${response.statusText}`);
//     }

//     // If the response is OK, parse the JSON
//     const responseBody = await response.json();  // Consume the stream and parse the JSON
//     console.log('Created issue:', responseBody);

//     // Return the created issue
//     return responseBody;

//   } catch (error) {
//     console.error('Error creating issue:', error);  // Log the error details
//     throw new Error('Failed to create issue in the target Jira instance');
//   }
// });  i am able to create the issue but i still get error in the console i have been trying to figure it out but i am confused whats going wrong

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events