How to read response data from Fetch in Forge app

We developed an app using Atlassian forge. we have one requirement to call an external service API in forge. We are using the Fetch API from ‘@forge/api’ package to make those API calls. But , Fetch always returns the response like {“headers”:{},“ok”:true,“status”:200,“statusText”:“OK”} instead of what we set up at service API level.

Tried both the await and promise approach but nothing changed.

Hi @IyyappanR welcome to the community. That certainly does seem like odd behavior - I would expect an error message if there was a configuration issue for instance. I’m on the team that implemented this feature and will try and help you figure this one out.

Could you provide code snippets from your app manifest file and a snippet of how you’re calling the fetch API in your code to help us reproduce it? Also, just for my understanding, what is the expected response from your service API? (status code, with or without a body, etc.)

Thanks for your response @ChrisWilliams . we are expect the response with body and status code details. but we are received the response in below format .
{“headers”:{},“ok”:true,“status”:200,“statusText”:“OK”}

code Snippet:
const AuthResponse = await fetch(‘http://localhost:9999/ExternalAuth/AuthClass’, {

  method: 'POST',

  headers: {

    'Accept': 'application/json',

    'Content-Type': 'application/json'

  },

  body: data

});

console.log("xrayAuthResponse "+AuthResponse);

As Forge applications are run on Atlassian’s infrastructure, you won’t be able to access resources hosted on your local machine such as on localhost. I’m not sure why you’re getting a 200 response here but you’re best off making requests to a publicly hosted URL on the internet either directly or via Forge External Authentication if you need authenticated requests.

@IyyappanR Just checking in again, it looks like you are printing out the resulting Fetch object, rather than the response data.

You will need to call either response.json() or response.text() to read what has been returned.

Reference node-fetch documentation for more information on what is in the resulting object.

2 Likes

We tested the fetch request from public hosted URL( deployed service in Azure app Serivice) as well. we are getting the response always {“headers”:{},“ok”:true,“status”:200,“statusText”:“OK”} instead of body of the response.

We used Forge Fetch API in forge instead of node-fetch. response.json() is not returning the response body while using the forge fetch API.

we will use node-fetch and let know status

Thanks for your response @AshleyBartlett

That’s interesting. You should be getting a fetch like object back. Which is why I passed you the docs for node-fetch.

You need to keep using forge API to be able to make calls.

What do you get when you call response.json()?

@IyyappanR Did you make any progress on this?

I had the same issue with the thread owner, I fixed that with the docs (Common Usage section). Thank you!

1 Like

Good day, just wanted to ask if there is a fix for this?

I am also having a problem with my call on custom UI since the response has no body.
I am using requestJira → createIssue on resolver.

When I am logging response.json() here is the result:
image

When I am logging response only here is the result:
image

On the network tab the response has an Id which is the one I need but I can’t read the data on the response body.

Here is my API call:
image

I got it fixed by using another then on the response.json().
image

In this case it’s not necessary to chain the promise, as you are already using async / await.

In javascript you will always need to await a promise object. Json is a promise, which I believe is to account for streams of responses, where it will wait for the entire body before parsing, to reduce the chance of errors.

If you wish to remove the then chaining from you code, you can write it like this:

const response = await api.asUser().requestJira(...);

if( ! response.ok) {
 throw new Error("Failed to get data");
}

return await response.json();