Linking issues using API

Hi All
I am trying to link issues using API in forge. The issues get linked however I get error that “Unexpected end of JSON input”. And I have attached the screen shot of the error.
Here is the code.

const attachToParent = async (issuesToBeLinked = []) => {
      console.log(
        attachToParent :: attaching tickets ${issuesToBeLinked.length} tickets
      );
      for (let i = 0; i < issuesToBeLinked.length; i++) {
        console.log(
          "attachToParent :: issuesToBeLinked :: ",
          issuesToBeLinked[i]
        );
  
        const bodyData = `{ 
          "inwardIssue": {
            "key": "TJP-196"
          },
          "outwardIssue": {
            "key": "TJP-195"
          },
          "type": {
            "name": "Duplicate"
          }
        }`;

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

   await attachToParent(['TJP-196']);

1 Like

@ianRagudo I would appreciate your input as you helped me in last issue :).

Hi @sushilb,

I was able to replicate your concern and found the problem in the code snippet you provided.

This line is causing the error but currently not yet sure why. However, to unblock you, you can simply remove this call and if you need to get the response’s status and status text, you can simply reference them with response.status and response.statusText, respectively.

A successful response body looks like this (and not sure what other response information you’d like to get).

{
    "headers":
    {},
    "ok": true,
    "status": 201,
    "statusText": "Created"
}

Hope this helps.

Ian

As an update, I raised this concern internally since the documentation shows the snippet wherein you can call await response.json() (but couldn’t in reality as the response body is empty based on response.text()). For now, to make your app work, given that the API call is successful, kindly refrain from calling response.json() only for this API if possible; you can get the status from the response directly.

Cheers,
Ian

@ianRagudo Thanks a lot for your help. Really appreciate it.

1 Like

Happy to help :slight_smile:

For me I needed to specify headers…

headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
    },

and the response was fine with json… seems like a bug since other api requests don’t require this