apichallenges > solutions > post-todos-400

How to complete the challenge POST /todos (400)

How to complete the challenge POST /todos (400) to fail to create a todo item in the application due to not passing validation.

POST /todos (400)

Issue a POST request to create a todo but fail validation on the doneStatus field

  • POST request will create a todo if the details are valid when using the /todos end point
  • 400 is an error code meaning that we supplied invalid details
  • In this case we are asked to make a mistake with the doneStatus field so that it fails validation server side i.e. doneStatus="bob"

Basic Instructions

  • Issue a POST request to end point "/todos"
    • http://apichallenges.eviltester.com/todos
  • The request should have an X-CHALLENGER header to track challenge completion
  • The content-type in the message should be application/json because we are sending a JSON payload
  • The Payload should have an error in the doneStatus. A valid doneStatus is true or false so if we send in a String like "bob" it should fail validation.
    {
        "title": "create new todo",
        "doneStatus": "bob",
        "description": "created via insomnia"
    }
  • The response status code should be 400 because the request is invalid
  • The body of the response will be an error message array with a single message
{
  "errorMessages": [
    "Failed Validation: doneStatus should be BOOLEAN"
  ]
}

Example Request

> POST /todos HTTP/1.1
> Host: apichallenges.eviltester.com
> User-Agent: rest-client
> X-CHALLENGER: x-challenger-guid
> Content-Type: application/json
> Accept: */*
> Content-Length: 116

|     {
|       "title": "create new todo",
|       "doneStatus": "bob",
|       "description": "created via insomnia"
|     }

Example Response

< HTTP/1.1 400 Bad Request
< Connection: close
< Date: Thu, 27 Aug 2020 14:23:12 GMT
< Content-Type: application/json
< X-Challenger: x-challenger-guid
< Server: Jetty(9.4.z-SNAPSHOT)
< Via: 1.1 vegur

Returned body:

{
  "errorMessages": [
    "Failed Validation: doneStatus should be BOOLEAN"
  ]
}

Overview Video

Watch on YouTube: "Solution to failing to create a Todo item using POST"

Patreon ad free version