|
| 1 | +--- |
| 2 | +date: 2021-01-30T15:55:00Z |
| 3 | +title: API Challenges Solution For - POST todos 400 extra |
| 4 | +description: How to solve API challenge POST todos 400 extra to trigger validation errors due to an extra field in the payload. |
| 5 | +--- |
| 6 | + |
| 7 | +# How to complete the challenge `POST /todos (400) extra` |
| 8 | + |
| 9 | +How to complete the challenge `POST /todos (400) extra` to fail to create a todo item in the application due to not passing validation when the payload contains an extra field. |
| 10 | + |
| 11 | +## POST /todos (400) extra |
| 12 | + |
| 13 | +> Issue a POST request to create a todo but fail validation due to an unrecognised field |
| 14 | +
|
| 15 | +- `POST` request will create a todo if the details are valid when using the `/todos` end point |
| 16 | +- `400` is an error code meaning that we supplied invalid details |
| 17 | +- In this case we are asked to make a mistake by adding an extra field not defined in the request schema e.g. `priority="extra"` |
| 18 | + |
| 19 | +## Basic Instructions |
| 20 | + |
| 21 | +- Issue a `POST` request to end point "/todos" |
| 22 | + - `{{<ORIGIN_URL>}}/todos` |
| 23 | +- The request should have an `X-CHALLENGER` header to track challenge completion |
| 24 | +- The `content-type` in the message should be `application/json` because we are sending a JSON payload |
| 25 | +- The Payload should have an error due to an unexpected field. |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "title": "a title", |
| 30 | + "priority": "extra" |
| 31 | +} |
| 32 | +``` |
| 33 | +- The response status code should be `400` because the request is invalid |
| 34 | +- The body of the response will be an error message array with a single message |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "errorMessages": [ |
| 39 | + "Could not find field: priority" |
| 40 | + ] |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Hints: |
| 45 | + |
| 46 | +- We don't just want to check for mandatory and missing content we need to make sure that the server does not try and create entities and inject new fields into the database |
| 47 | +- For follow on exercises you might want to see what happens: |
| 48 | + - if we duplicate fields e.g. have two `title` fields |
| 49 | + - if we duplicate headers |
| 50 | +- When testing APIS we need to go beyond field contents and look at the message format itself |
| 51 | + |
| 52 | +## Example Request |
| 53 | + |
| 54 | +~~~~~~~~ |
| 55 | +> POST /todos HTTP/1.1 |
| 56 | +> Host: {{<HOST_URL>}} |
| 57 | +> User-Agent: rest-client |
| 58 | +> X-CHALLENGER: x-challenger-guid |
| 59 | +> Content-Type: application/json |
| 60 | +> Accept: */* |
| 61 | +> Content-Length: 116 |
| 62 | +
|
| 63 | +| { |
| 64 | +| "title": "a title", |
| 65 | +| "priority": "extra" |
| 66 | +| } |
| 67 | +~~~~~~~~ |
| 68 | + |
| 69 | +## Example Response |
| 70 | + |
| 71 | +~~~~~~~~ |
| 72 | +< HTTP/1.1 400 Bad Request |
| 73 | +< Connection: close |
| 74 | +< Date: Thu, 27 Aug 2020 14:23:12 GMT |
| 75 | +< Content-Type: application/json |
| 76 | +< X-Challenger: x-challenger-guid |
| 77 | +< Server: Jetty(9.4.z-SNAPSHOT) |
| 78 | +< Via: 1.1 vegur |
| 79 | +~~~~~~~~ |
| 80 | + |
| 81 | +Returned body: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "errorMessages": [ |
| 86 | + "Could not find field: priority" |
| 87 | + ] |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments