Create issue with Jira API using NodeJS Express

Using Jira Create issue Documentation:
https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-post

Running POST request in Postman, receiving Bad Request, please help!

*server side:

const express = require("express");
const axios = require("axios");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();

app.use(bodyParser.json());
app.use(cors());
const email = "*********";
const token = "*********";
const baseUrl = "*********";

const jiraRequestHeaders = {
  Authorization: `Basic ${Buffer.from(`${email}:${token}`).toString("base64")}`,
};

app.post("/createIssue", async (req, res) => {
  console.log("create issue");
  await axios
    .post(`${baseUrl}rest/api/2/issue`, {
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        ...jiraRequestHeaders,
      },
      body: bodyData
    .then((text) => {
      console.log(`Response: ${response.status} ${response.statusText}`);
      res.sendStatus(200);
    })
    .catch((err) => {
      console.log(JSON.stringify(err));
      res.sendStatus(err.response.status);
    });
});

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000...`);
});

*client side:

function createIssue() {
//create body data per each parameter

const bodyData = JSON.stringify ({
    "update": {},
    "fields": {
      "summary": document.getElementById("summary"),
      "issuetype": {
        "id": document.getElementById("issuesOption")
      },
      "components": [
        {
          "id": document.getElementById("components")
        }
      ],
      "project": {
        "id": document.getElementById("projectOption")
      },
      "description": {
        "type": "doc",
        "version": 1,
        "content": [
          {
            "type": "paragraph",
            "content": [
              {
                "text": "Order entry fails when selecting supplier.",
                "type": "text"
              }
            ]
          }
        ]
      },
      "reporter": {
        "id": "61795759860f78006b3a16b9"
      },
      "priority": {
        "id": document.getElementById("issuesPriorities")}
      },

      "assignee": {
        "id": document.getElementById("assigneeOption")
      }
});
document.body.innerHTML = bodyData;
};

*html:

<body>
    <button id="createIssue" onclick="CreateIssue()">Create</button>
</body>

Hello @AnnaVoloshin ,

If I understand correctly you are not able to make this work in POSTMAN too (you get 400 Bad Request). In this is the case there might be something wrong with the request body, so please paste it in here, using the preformatted text option (shortcut: CTRL+E) like in below example:

{
      "fields": {
        "project": {
          "key": "TKPF"
        },
        "summary": "Create issue with label",
        "issuetype": {
          "name": "Task",
          "subtask": false
        },
        "priority": {
          "name": "Medium"
        },
        "labels" : ["label"]
      }
}

If this is not the case please provide more details.

Also, just FYI, please notice that you are pointing to the documentation for the REST API v3 issue endpoint (rest/api/3/issue) while in the code you pasted (that got a bit messed-up since you didn’t use the preformatted text) I can see you are calling the v2 endpoint (rest/api/2/issue).

Cheers,
Dario

1 Like

After reviewing the updated post (using preformatted text) I can see that in your request body the JSON payload starts with “update”, that is actually supposed to be used only when editing/updating an existing issue by sending a PUT request to the same endpoint and this can be what is causing the issue here.

Can you kindly remove it and see if it works?