6

I am using axios in reactjs application. My code is like below

axios.post('/api/addresses/upload/',formData,config)
  .then(function (response) {

  })
  .catch(error => {
  });

I did not use any console.log() statement any where but I am getting below error in console.

enter image description here

How this error printed in console ?

abu abu
  • 6,599
  • 19
  • 74
  • 131

1 Answers1

1

Try by setting the header for the axios request,

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.

May be you will be missing some of your required fields which is mandatory while processing the operations like insertion or updation of a record in your Database. May be you can have the look at it. If everything works well then try the following settings to the header of your request.

If your submitting the form with files then use the header's content-type as

  1. 'multipart/form-data',
  2. without files means then set the content-type as 'application/x-www-form-urlencoded',
  3. If you need to post the json means then set the content-type as 'application/json' and "Accept: 'application/json'"

If any cors error occurs then use 'crossDomain': true

var formData = new FormData();
axios.post('/api/addresses/upload/', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})
Jeeva
  • 1,550
  • 12
  • 15
  • A server application should return a 400 Bad Request if mandatory fields are missing because missing fields indicate a malformed request. 422 Indicates a well formed request which semantically doesn't make sense. It is very rarely used and usually only in the context of a WebDAV service. Also OP asked why this error is logged to the console and not how to resolve it. So this does not answer the question. – trixn Jun 14 '18 at 11:41
  • He is posting his data right, I have said in the following point of view. Let say, he is trying to post something like as follows { "name": "XXX", "age": "5"}. The object looks right, that is it requires the name and age to insert a record, but he might have set his field "name" as string and "age" as number in his database. In that scenario, the above object is fine. But the age posted as string, at the moment it will throw 422 Unprocessable Entity response status code since it requires a number instead of string. – Jeeva Jun 14 '18 at 11:51
  • A wrong type for a field also indicates a malformed request and therefore 400. 422 would only apply if all required fields are present and of the correct type but do not semantically make any sense. How it can't make sense is very specific to the use case. Something like you are creating a new calendar entry on 29. february in a year that doesn't have one. – trixn Jun 14 '18 at 11:58
  • 1
    The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions. https://stackoverflow.com/questions/16133923/400-vs-422-response-to-post-of-data – Jeeva Jun 14 '18 at 12:12
  • Still it was not what OP asked for. So it is not an answer to the question. – trixn Jun 14 '18 at 12:13