我正在嘗試將我的應用程式連接到外部 api。外部 api 的檔案說使用以下方式進行呼叫curl
:
curl "https://wordsapiv1.p.mashape.com/words/soliloquy"
-H "X-Mashape-Key: <required>"
我的應用程式是用 JS/React 構建的——我不熟悉上述語法。我試圖弄清楚如何使用superagent
(或者,如果需要,fetch
)來撰寫它。
到目前為止,這是我沒有遵循其他一些 SO 答案的結果,但我不知道如何合并: -H "X-Mashape-Key: <required>"
我的 api 密鑰存盤在一個.env
檔案中。
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)
})
}
uj5u.com熱心網友回復:
我強烈建議您熟悉curl
. 這是值得知道的,它無處不在。你可以在這里和這里找到不錯的備忘單。
-H
incurl
代表一個標頭,因此在您的情況下,您需要將其添加到您的請求中,例如
request.get(`${url}/dog`)
.set('X-Mashape-Key', API_KEY)
.then((response) => {...
或獲取:
fetch("https://wordsapiv1.p.mashape.com/words/soliloquy", {
headers: {
"X-Mashape-Key": API_KEY
}
})
作為獎勵,我找到了一個不錯的專案,可以“翻譯”curl
為fetch
:https ://kigiri.github.io/fetch/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505184.html
標籤:javascript api 卷曲 拿来 超级代理