要訪問我的 api,我需要在 axios get 請求中添加一個標頭欄位:x-api-key,其值作為 api 鍵:12345678。如何才能對我當前的代碼做出反應?
import "./App.css";
import axios from "axios";
import { useEffect } from "react";
function App() {
useEffect(() => {
axios
.get("https://challenge.movies.com.au/api/v2/blahblah/movies")
.then((res) => console.log(res.data))
.catch((err) => console.log(err));
}, []);
return <div className="App"></div>;
}
export default App;
uj5u.com熱心網友回復:
試試這個。
const AuthStr = 'Bearer '.concat(USER_TOKEN);
axios.get(URL, { headers: { Authorization: AuthStr } })
.then(response => {
// If request is good...
console.log(response.data);
})
.catch((error) => {
console.log('error ' error);
});
uj5u.com熱心網友回復:
這是 axios 上標頭的用法:
useEffect(() => {
axios.get("https://challenge.movies.com.au/api/v2/blahblah/movies", {
headers:{
'x-api-key': 'Bearer 12345678')
}
.then((res) => console.log(res.data))
.catch((err) => console.log(err));
}, []);
uj5u.com熱心網友回復:
你可以簡單地傳遞這樣的標題
axios
.post(
"url",
{
key : value
},
{
headers: {
Authorization: "Bearer " JSON.parse(token),
},
}
)
.then(async (response) => {
//Handel response here
})
.catch((error) => {
console.log(error);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/464900.html
上一篇:在導航容器內的堆疊之間導航
