[英]How to write request body with axios?
I'm using firebase-auth and trying to get user data with axios.我正在使用 firebase-auth 并尝试使用 axios 获取用户数据。
I can get response successfully when I use curl command like this.当我像这样使用 curl 命令时,我可以成功获得响应。
curl 'https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=[API_KEY]' \
-H 'Content-Type: application/json' --data-binary '{"idToken":"[FIREBASE_ID_TOKEN]"}'
https://firebase.google.com/docs/reference/rest/auth#section-get-account-info https://firebase.google.com/docs/reference/rest/auth#section-get-account-info
I'm wondering how to change this request using by axios. I tried but it didn't work.我想知道如何使用 axios 更改此请求。我试过了,但没有成功。
const url = 'https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=key
const token = localStorage.getItem('token');
const body = {"idToken":token}
axios
.get(url,token, {
headers: {'Content-Type': 'application/json'}, data: body
})
.then(res => {
console.log(res.data);
}).catch(error => {
alert("error");
})
You cannot add body in a GET request, change it to post and add as the below line您不能在 GET 请求中添加正文,将其更改为发布并添加为以下行
let response = await axios.post(url,{data:body});
You can not pass the body in GET request so you need to use Axios POST request for pass token in body parameters.您不能在 GET 请求中传递正文,因此您需要在正文参数中使用 Axios POST 请求传递令牌。 If you need to use GEt request for same so pass the token in url.
如果您需要使用 GEt 请求,请在 url 中传递令牌。
const token = localStorage.getItem('token');
const config = {
headers: { Authorization: `Bearer ${token}` }
};
const body = {"idToken":token}
axios.post(
'https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=key',
bodyParameters,
config
).then(console.log).catch(console.log);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.