如何用axios写请求体?

How to write request body with axios?

我正在使用 firebase-auth 并尝试使用 axios 获取用户数据。

像这样使用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

我想知道如何使用 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");
                })

您不能在 GET 请求中添加正文,将其更改为 post 并添加如下行

let response = await axios.post(url,{data:body});

您不能在 GET 请求中传递正文,因此您需要使用 Axios POST 请求在正文参数中传递令牌。如果您需要使用 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);