Home > Enterprise >  understanding the error response to an http request to rest api
understanding the error response to an http request to rest api

Time:02-10

This is a simple request to a REST API using AXIOS:

  await axios.request({
    url: 'https://gorest.co.in/public/v2/users',
    method: 'POST',
    data: {
      "id":2925,
      "name":"Himani Rana",
      "email":"[email protected]",
      "gender":"female",
      "status":"inactive"})
   .catch(error => {
      console.log(error);
    });
  }

This EMAIL is ALREADY taken. So this raises an exception because all users must have unique emails.

This code logs the following error:

{
"message": "Network Error",
"name": "Error",
"fileName": "https://c.staticblitz.com/d/webcontainer.a330fe2aef0d6950e5edf5ad2e9521381ac64c16.js line 15 > eval",
"lineNumber": 3,
"columnNumber": 8750,
"stack": "e.exports@https://typescript-9ggya9.stackblitz.io/turbo_modules/[email protected]/dist/axios.min.js:3:8750\ne.exports/</g.onerror@https://typescript-9ggya9.stackblitz.io/turbo_modules/[email protected]/dist/axios.min.js:3:7558\n",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"Authorization": "Token 1a459dd3d2e13a989f01565f5f1ba32350f8216897bb7fee5493feb210a06"
},
"url": "https://gorest.co.in/public/v2/users",
"method": "post",
"data": "{\"id\":2925,\"name\":\"Himani Rana\",\"email\":\"[email protected]\",\"gender\":\"female\",\"status\":\"inactive\"}"
},
"status": null
}

This is difficult to read and when I went through it carefully, I did not see something that helps me identify the error apart from a message that says "Network Error".

NOW, doing this http request with POSTMAN or THUNDER CLIENT simply shows:

[
  {
    "field": "email",
    "message": "has already been taken"
  }
]

How do these REST clients identify the error properly but AXIOS doesn't?

CodePudding user response:

From what I can understand, the problem is in axios library.

Try to reinstall axios or use the Fetch API instead (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

P.S: Nowadays, Fetch API is more preferred, than axios.

  • Related