1

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));
  • Please paste the link to the documentation that you used – Konrad Sep 13 '22 at 16:05
  • https://blog.developer.atlassian.com/creating-a-jira-cloud-issue-in-a-single-rest-call/ this and https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-group-issues this – Roberto Teresa Sep 13 '22 at 17:29

1 Answers1

2

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"
              }
            ]
          }
        ]
      }
    }
  });
Konrad
  • 21,590
  • 4
  • 28
  • 64