Skip to content Skip to sidebar Skip to footer

No Json Object With Fetch()

I have a oauth set up. But when I want to get the access token with the fetch() function it just returns an object with things like _bodyInit, _bodyBlob and headers. So I just cann

Solution 1:

You almost got it right, you are missing one step though!

fetch doesn't return a json object, it returns a Response object, in order to get the json object, you have to use res.json()

fetch(`https://dribbble.com/oauth/token`, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          'client_id': 'MY_ID',
          'client_secret': 'MY_SECRET',
          'code': code
        })
      })
      .then((res) => {
        return res.json();
      })
      .then((json) => {
         console.log(json); // The json object is here
      });

It's a good practice to add a catch just in case something goes wrong.

.then((json) => {
      console.log(json); // The json object is here
 });
.catch((err) => {
     // Handle your error here.
})

Post a Comment for "No Json Object With Fetch()"