Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

wordsAPI fetch returns 200 status with no data javascript

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
    }
}  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading