Search code examples
jsonposthttp-status-code-422

Getting 422 from server - am I passing JSON object wrongly?


I am working on a React project that pushes data to a node.js server running on localhost. The actual code is here:

task = {text:e.target.value};
        console.log(task.text);
        task = JSON.stringify(task);
        console.log(task.text);
        console.log(task);

        axios.post('http://192.168.0.116:8080/todos', {task})
            .then(response => {console.log(response);
            this.axGetTasks(response.data);
            })
            .catch(error => {
                if (error.response) {
                    console.log(error.response);
                }
            })

The server returns a 422 with this explanation: {data: "'text' field must be present in json".

Console.log of object I am passing:

{"text":"gdf"}

The server is running this test:

(!if.req.body.text)

I guess I am malforming the input, but I cannot see the mistake - especially as I am relying on JSON.stringify. Have I made a mistake when creating the task object? When I try console.log(task.text); it returns undefined - but I have no idea why, as I have minimal experience with JSON. I believe that the format I am using is conforming with the standards. I will be grateful for any input!

EDIT: how is JSON object attached to the URL? Is it through "?" as a parameter?


Solution

  • Axios does stringify itself. Removing stringifying solved the problem.