Axios VS Postman请求获得不同的响应

问题描述 投票:0回答:1

大家好,我遇到了一个与API连接有关的奇怪问题。

虽然我使用Postman时一切正常,但是如果我通过Axios发出请求,则响应会有所不同。因此,举一个例子,我想获得一个ID为616的客户端;

客户

await axios
        .post("/api/getClient", {
          id: 616,
        }).then(data => console.log(data))

后端

// GET CLIENT
app.post('/api/getClient', async (req, res) => {
  const d = await axios.get(`${baseURL}/client/account/:client_id`, {
    headers: headers,
    data: {
      id: req.body.id
    }
  })
  res.send(d.data)
  res.end()
});

来自axios的响应看起来像这样

data: {
  error: false,
  user: {
    profile: null,
    user_addresses: [],
    safe_credits: 0,
    rating: 5
  }
}

来自邮递员

{
    "error": false,
    "user": {
        "profile": {
            "id": 616,
            "phone_number": "07744444444",
            "country_code": "44",
            "email": "[email protected]",
            "first_name": "John",
            "last_name": "Doe",
            "password": null,
            "os": "iOS",
            "token": "0c9c09e7dc2f009b4cfdb2e4666ead9e",
            "version": "",
            "photo": "",
            "gender": "male",
            "registered_date": "2020-05-08T14:55:05.000Z",
            "enabled": 1,
            "socketId": "",
            "stripe_id": "cus_HF1eUUdada54JKN"
        },
        "user_addresses": [],
        "safe_credits": 0,
        "rating": 5
    }
}
javascript axios postman
1个回答
0
投票

而不是在请求中使用data,您只需将ID附加到您的API URL:

 const d = await axios.get(`${baseURL}/client/account/${req.body.id}`, {
   ...
 });
© www.soinside.com 2019 - 2024. All rights reserved.