简体   繁体   English

如何用axios写请求体?

[英]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.

相关问题 如何在 React 项目中使用 axios 和 Firebase 的删除请求? - How to use delete request with axios in react project and Firebase? JMeter中后续请求如何去掉request body - How to remove request body from subsequent requests in JMeter HTTP 带有请求正文的 POST - HTTP POST with request body Axios get Request收不到数据 - Axios get Request not receiving data 如何在 req.body 中发送 post 请求后读取文件 - How to read files after sending a post request in the req.body 如何使用 enrich with foreach/iterate 修改 SOAP 请求体? - How to use enrich with foreach/iterate to modify SOAP request body? 如何在azure api管理策略表达式中查看请求体是null? - How to check request body is null in azure api management policies expression? Azure API 管理 - 如何将正文与我的请求一起发送 - Azure API Management - How to send body along with my request 如何在使用来自 AWS Lambda 的 307 重定向(POST 方法和正文)在 python 中传递请求正文 - How to do pass a Request Body while doing a 307 Redirect with a( POST Method and body ) from AWS Lambda , in python Lambda 正在请求正文中返回标头 - Lambda is returning headers in body of request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM