I am trying to wire up my app to an external api. The external api's documentation says to make the call using curl
:
curl "https://wordsapiv1.p.mashape.com/words/soliloquy"
-H "X-Mashape-Key: <required>"
My app is built with JS/React - I am unfamiliar with the above syntax. I am trying to figure out how to write this using superagent
(or, if necessary, fetch
).
This is what I have so far from following some other SO answers but I have no idea how to incorporate: -H "X-Mashape-Key: <required>"
My api key is stored in a .env
file.
require('dotenv').config
console.log(process.env)
const request = require('superagent')
const url = 'https://wordsapiv1.p.mashape.com/words/'
//dictionary
export function getDictionaryDefinition(word) {
return request.get(`${url}/dog`).then((response) => {
console.log(response)
})
}
CodePudding user response:
I highly recommend getting familiar with the basics of curl
. It's worth knowing and it's everywhere. you can find nice cheat sheets here and here.
-H
in curl
represent a header, so in your case you'll need to add that to your request, e.g.
request.get(`${url}/dog`)
.set('X-Mashape-Key', API_KEY)
.then((response) => {...
or in fetch:
fetch("https://wordsapiv1.p.mashape.com/words/soliloquy", {
headers: {
"X-Mashape-Key": API_KEY
}
})
as a bonus, I found a nice project that "translates" curl
to fetch
: https://kigiri.github.io/fetch/