1

I am trying to populate a already built php database using axios requests, however i keep receiving 422 error and I don't understand what I am missing. Could you please help me :) This is the error that i get:

xhr.js:177 POST URL/data 422 (Unprocessable Entity)

This is the Post request schema of the DB:

"post": {
    "summary": "Post new data row",
    "description": "Post new data row",
    "requestBody": {
      "required": true,
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "data": {
                "type": "string",
                "example": "{\"test\":1}"
              },
              "type": {
                "type": "string",
                "example": "1"
              },
              "status": {
                "type": "integer",
                "example": 1
              }
            },
            "required": [
              "data",
              "type"
            ]
          }
        }
      }
    },
"responses" :{
 "422": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "error": {
                    "type": "string",
                    "example": "Invalid input"
                  }

This is my code, I have several switch cases and they should all work in the same manner:

 case "rezervariContact" : {
            const {type, titlu, description, phone, email} = this.state;
            const contactData = {
                type: this.state.type,
                data:{
                    type, 
                    data :{
                        titlu, description, phone, email
                    }
                },
                status:true
            }
            data = {...contactData};
        }
    }
    await axios({
        method: "post",
        headers: { "Content-Type": "application/json",
                    'Accept': '*/*' },
        body:data,
        url : 'http://xxxxxxxxx.ro/data'
      })
        .then(function (response) {
          console.log("RespResolved::",response.config.params);
        })
        .catch(function (response) {
          console.log("catchErrResp::",response);
        });
}

Please if you can spot something let me know.

Anad Dana
  • 81
  • 3
  • 10

2 Answers2

1

422 probably means your inputted data is invalid.

The reason for this depends on the server. You could try referring to any documentation for the API/server you are trying to reach.

M3D
  • 673
  • 6
  • 11
isuckatcode
  • 76
  • 1
  • 6
  • MDN is explaining the error like this: 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. – Anad Dana Jul 26 '21 at 09:16
  • @AnadDana In Laravel when you validate data & something's not right, it sends back a 422 response. So I figured out it might be it due to the JSON response having things like ` "error": { "type": "string", "example": "Invalid input" }` – isuckatcode Jul 26 '21 at 09:22
  • hmm, any idea what is invalid about my data? :)I can't really see the problem here :( – Anad Dana Jul 26 '21 at 09:25
  • If it is in fact Laravel, did you check your validation method? Maybe the problem is there – isuckatcode Jul 26 '21 at 09:29
  • i don't know how to do this – Anad Dana Jul 26 '21 at 09:34
  • If the API is in Laravel, it may have something to do with the data you are sending. Try sending data from an API client like Insomnia or Postman to check if there's something wrong with the data you are sending. If that does not work, I would probably check the validation methods by going to the controller & reviewing the validation section. – isuckatcode Jul 26 '21 at 09:49
  • Will do so.Thanks a lot for your input ;) – Anad Dana Jul 26 '21 at 09:50
  • I got this problem, when I tried to save data, that contained an email address. The email address field in the DB was set to "unique". – Robert Wildling Dec 13 '21 at 22:17
0

Check document on http://xxxxxxxxxx.ro/data! It means your post data is valid but server can not hand it correctly!

Anad Dana
  • 81
  • 3
  • 10
Jamviet.com
  • 307
  • 3
  • 11