Home > Software design >  wordsAPI fetch returns 200 status with no data javascript
wordsAPI fetch returns 200 status with no data javascript

Time:03-12

I have just subbed to wordsAPI https://www.wordsapi.com/

and pasted the code to fetch data straight from their Docs:

const getDefinition = async (text) => {
    try {
        const res = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${text}/definitions`, {
            "method": "GET",
            "headers": {
                "x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
                "x-rapidapi-key": "MYAPIKEY"
            }
        })
        console.log(res)
    } catch (err) {
        console.log(err)
    }
}

the response object looks as such:

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://wordsapiv1.p.rapidapi.com/words/something/synonyms"
[[Prototype]]: Response

I get back a 200 but the response has no data?

CodePudding user response:

You should use res.json() to fetch data:

const getDefinition = async (text) => {
try {
    const res = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${text}/definitions`, {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
            "x-rapidapi-key": "MYAPIKEY"
        }
    })
    const mainData = await res.json();
    console.log(mainData)
} catch (err) {
    console.log(err)
    }
}  
  • Related