Search code examples
node.jscypressjira

Jira error when creating issue in node.js


The following code gives me the error "Invalid request payload. Refer to the REST API documentation and try again" when is executed and I dont know where the error is

const bodyData = 
    `"fields": {
        "summary": "Summit 2019 is awesome!",
        "issuetype": {
            "name": "Task"
        },
        "project": {
            "key": "PRB"
        },
        "description": {
            "type": "doc",
            "version": 1,
            "content": [
                {
                "type": "paragraph",
                "content": [
                    {
                    "text": "This is the description.",
                    "type": "text"
                    }
                ]
                }
            ]
        }
    }`;

fetch('https://mysite.atlassian.net/rest/api/3/issue', {
    method: 'POST',
    headers: {
        'Authorization': `Basic ${Buffer.from('myemail@gmail.com:mytoken').toString('base64')}`,
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: bodyData
}).then(response => {
        console.log(
            `Response: ${response.status} ${response.statusText}`
        );
        return response.text();
    }).then(text => console.log(text)).catch(err => console.error(err));

Solution

  • Problem

    Your bodyData is not a valid json

    Solution

    Wrap it with {}

      `{
        fields: {
          "summary": "Summit 2019 is awesome!",
          "issuetype": {
            "name": "Task"
          },
          "project": {
            "key": "LOD"
          },
          "description": {
            "type": "doc",
            "version": 1,
            "content": [
              {
                "type": "paragraph",
                "content": [
                  {
                    "text": "This is the description.",
                    "type": "text"
                  }
                ]
              }
            ]
          }
        }
      }`
    

    P.S. You would find this error if you would use JSON.stringify with an object instead of a string.

    const bodyData = JSON.stringify(
      {
        fields: {
          "summary": "Summit 2019 is awesome!",
          "issuetype": {
            "name": "Task"
          },
          "project": {
            "key": "LOD"
          },
          "description": {
            "type": "doc",
            "version": 1,
            "content": [
              {
                "type": "paragraph",
                "content": [
                  {
                    "text": "This is the description.",
                    "type": "text"
                  }
                ]
              }
            ]
          }
        }
      });