Search code examples
javascriptfetch

Not getitng the same response in postman and fetch


I have this function to make a request to gyphy api

const getGifs = async () => {
        const url = 'https://api.giphy.com/v1/gifs/search?api_key=mykey&q=ps5&limit=5';
        const resp =  await fetch(url)
            .then(response => console.log(response));
        
}

In postman I get a json with the searched data but in javascript I get a response object, how can I get the searched data?


Solution

  • The fetch API does not return the raw response a such. The object you're getting is one that can be transformed into what you need. Since you're expecting JSON data, then your code should be:

    const getGifs = async () => {
            const url = 'https://api.giphy.com/v1/gifs/search?api_key=mykey&q=ps5&limit=5';
            const resp =  await fetch(url)
                .then(response => response.json())
                .then(jsonData => console.log(jsonData)) // the response you're expecting
            
    }
    

    The .json() method returns a Promise that resolves with your JSON parsed data.