如何在React Axios请求中添加带有值为API键的标题字段
要访问我的API,我需要添加一个标题字段:X-API-KEY在Axios中获取以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;
To access my api I need to add a header field: x-api-key to the axios get request with the value as the api key: 12345678.
How can I do this in react with my current code?
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下。
Try this.
这是Axios上的标头的用途:
here is the use for headers on axios:
您可以像这样通过
You can simply pass headers like that