javascript - how to get axios error postrequest payload on catch - Stack Overflow

when I catch an axios error, how can i get the request body? (what I've sent to the server, not th

when I catch an axios error, how can i get the request body? (what I've sent to the server, not the server response)

example of what i want to achieve

axios.post("url",{
    name:jesus
}).then((response) => {
    ....
}).catch((error) => {
    console.log(error.request.body)  //name:jesus
})

when I catch an axios error, how can i get the request body? (what I've sent to the server, not the server response)

example of what i want to achieve

axios.post("url",{
    name:jesus
}).then((response) => {
    ....
}).catch((error) => {
    console.log(error.request.body)  //name:jesus
})
Share asked Jun 16, 2022 at 23:01 Jesus David Liang ChenJesus David Liang Chen 1073 silver badges9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

You can access the original request data via error.config.data. Note that it will be the serialised format (in your case JSON) so you may need to parse it back into a JS object.

axios
  .post("http://httpbin/status/400", {
    name: "jesus",
  })
  .then((res) => {
    console.log("response", res);
  })
  .catch((error) => {
    const data = JSON.parse(error.config.data);
    console.error("request data", data);
  });
<script src="https://cdn.jsdelivr/npm/axios/dist/axios.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745044871a4608033.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信