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
1 Answer
Reset to default 7You 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条)