Forge - Making request to Jira API to create an issue (using form)

Hey everyone,

I am developing an app based on the jira-issue-action UI Kit, and I am having some trouble to make the POST API call to create an issue after the user fills up this form (I am also having trouble to get the form data, but I have tried to call the API without the form interfering with functioning):

I have tried different ways of calling the API (different styles of body, headers, different versions of API, asApp, asUser), none of which worked, so after I spent the last week on this and after I thoroughly read the documentation and still could not get it to work I decided to ask the community for some help.

Edit: I forgot to mention that the data I need to create an issue (summary, etc) I already have, I am just creating a bug with a different description but linked to the original issue.

Thank you in advance!

1 Like

Can you share some code snippets so that we can see what it looks like? And can you also link to the documentation for the API endpoint you are trying to make the request to?

Thanks for your reply!

I am calling this function inside the onSubmit method I passed to the form:

I have tried with both /api/2 and /api/3.

const response = await api.asApp().requestJira(route`/rest/api/3/issue`, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: bodyData
  });
  
  console.log(`Response: ${response.status} ${response.statusText}`);
  console.log(await response.json());

This is the form:

<ModalDialog header="Create a bug from this issue" onClose={() => setOpen(false)}>
      {/* <Text>{issue.summary} - {issue.project.key} - {globalIssueKey} - {issue.priority.id}</Text> */}
      <Form onSubmit={onSubmit} submitButtonText="Create">
        <TextArea name="description" label="Description"></TextArea>
      </Form>
    </ModalDialog>

And this is the body of the post request:

var bodyData = `{
    "fields": {
       "project":
       {
          "key": "TDG"
       },
       "summary": "REST ye merry gentlemen.",
       "description": "Creating of an issue using project keys and issue type names using the REST API",
       "issuetype": {
          "name": "Bug"
       }
   }
}`;

I have also tried with different “bodies” for the request.

Some of documentation I have looked up are in these links:

The Jira Cloud platform REST API (atlassian.com)

I should mention I am using forge, but I have been doing so only for the past couple of weeks, when I started developing this app.

2 Likes

I am posting the remainder of the documentation I have looked up here:

Jira REST API examples (atlassian.com)

JIRA REST API Example Create Issue 7897248 (atlassian.com)

1 Like

And what are you seeing logged?

I have changed the body of my request to almost match a GET request I made to an issue already created. (I had to remove a lot of fields from from the response I got from the GET request because the logs instructed me to).

This is what I have left (and is being used as body):

{
    "id": "10004",
    "self": "https://goncalodev.atlassian.net/rest/api/2/issue/10004",
    "key": "TDG-4",
    "fields": {
      "issuetype": {
        "self": "https://goncalodev.atlassian.net/rest/api/2/issuetype/10001",
        "id": "10001",
        "description": "Functionality or a feature expressed as a user goal.",
        "iconUrl": "https://goncalodev.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium",
        "name": "Story",
        "subtask": false,
        "avatarId": 10315,
        "hierarchyLevel": 0
      },
      "project": {
        "self": "https://goncalodev.atlassian.net/rest/api/2/project/10000",
        "id": "10000",
        "key": "TDG",
        "name": "TesteDG",
        "projectTypeKey": "software",
        "simplified": false,
        "avatarUrls": {
          "48x48": "https://goncalodev.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10413",
          "24x24": "https://goncalodev.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10413?size=small",
          "16x16": "https://goncalodev.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10413?size=xsmall",
          "32x32": "https://goncalodev.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10413?size=medium"
        }
      },
      "priority": {
        "self": "https://goncalodev.atlassian.net/rest/api/2/priority/2",
        "iconUrl": "https://goncalodev.atlassian.net/images/icons/priorities/high.svg",
        "name": "High",
        "id": "2"
      },
      "labels": [],
      "assignee": {
        "self": "https://goncalodev.atlassian.net/rest/api/2/user?accountId=62fceb401e82e839c25008fc",
        "accountId": "62fceb401e82e839c25008fc",
        "emailAddress": "goncalo.pereira2@adidas.com",
        "avatarUrls": {
          "48x48": "https://secure.gravatar.com/avatar/eb6867238200154625cda6a68d890029?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FG-1.png",
          "24x24": "https://secure.gravatar.com/avatar/eb6867238200154625cda6a68d890029?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FG-1.png",
          "16x16": "https://secure.gravatar.com/avatar/eb6867238200154625cda6a68d890029?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FG-1.png",
          "32x32": "https://secure.gravatar.com/avatar/eb6867238200154625cda6a68d890029?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FG-1.png"
        },
        "displayName": "Goncalo.Pereira2",
        "active": true,
        "timeZone": "Europe/Lisbon",
        "accountType": "atlassian"
      },
      "components": [],
      "description": null,
      "attachment": [],
      "summary": "If it worked, success!!",
    }
  }

And the logs are as follows:

INFO    2022-08-29T20:06:18.781Z 14c4a074-e1f5-4a14-b08a-c5fc3f9cc740 Response: 400 Bad Request
INFO    2022-08-29T20:06:18.781Z 14c4a074-e1f5-4a14-b08a-c5fc3f9cc740 {
  errorMessages: [
    'There was an error parsing JSON. Check that your request body is valid.'
  ]
}
1 Like

I have actually managed to perform the request today. I have changed a few things and used this request as a test:

  var bodyData = `
  {
    "fields": {
       "project":
       {
          "key": "${projectKey}"
       },
       "summary": "REST ye merry gentlemen.",
       "description": "Creating of an issue using project keys and issue type names using the REST API",
       "issuetype": {
          "name": "Bug"
       }
   }
}
    `

Now I just need to sort out the form, but I’ll create a new topic for that.

Thank you for the help!

2 Likes