Skip to content

Commit

Permalink
additional solution
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltester committed Dec 31, 2024
1 parent 8e24452 commit fdc4e60
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ public static ChallengeDefinitionData postTodosInvalidExtraField400(int challeng
"Issue a POST request to create a todo but fail validation because your payload contains an unrecognised field.");

aChallenge.addHint("Try to create a todo with a title, description and a priority");
// aChallenge.addSolutionLink("Read Solution", "HREF", "https://www.eviltester.com/apichallenges/howto/post-todos-400");

aChallenge.addSolutionLink("Send a POST request to /todos with a priority field e.g. {\"title\":\"a title\",\"priority\":\"extra\"}", "", "");
aChallenge.addSolutionLink("Read Solution", "HREF", "/apichallenges/solutions/post-create/post-todos-400-extra-field");

// aChallenge.addSolutionLink("Watch Insomnia Solution", "YOUTUBE", "tlye5bQ72g0");
// TODO: create solution for unrecognised field names
// TODO: create video solution for unrecognised field names
return aChallenge;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ description: A list of all the solutions for the API Challenges. Try them yourse
- [POST /todos (400) description too long](/apichallenges/solutions/post-create/post-todos-400-description-too-long)
- [POST /todos (201) max out content](/apichallenges/solutions/post-create/post-todos-201-max-content)
- [POST /todos (413) content too long](/apichallenges/solutions/post-create/post-todos-413-content-too-long)
- POST /todos (400) extra
- [POST /todos (400) extra](/apichallenges/solutions/post-create/post-todos-400-extra-field)

## Creation Challenges with PUT

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
date: 2021-01-30T15:55:00Z
title: API Challenges Solution For - POST todos 400 extra
description: How to solve API challenge POST todos 400 extra to trigger validation errors due to an extra field in the payload.
---

# How to complete the challenge `POST /todos (400) extra`

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.

## POST /todos (400) extra

> Issue a POST request to create a todo but fail validation due to an unrecognised 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 by adding an extra field not defined in the request schema e.g. `priority="extra"`

## Basic Instructions

- Issue a `POST` request to end point "/todos"
- `{{<ORIGIN_URL>}}/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 due to an unexpected field.

```json
{
"title": "a title",
"priority": "extra"
}
```
- 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

```json
{
"errorMessages": [
"Could not find field: priority"
]
}
```

Hints:

- 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
- For follow on exercises you might want to see what happens:
- if we duplicate fields e.g. have two `title` fields
- if we duplicate headers
- When testing APIS we need to go beyond field contents and look at the message format itself

## Example Request

~~~~~~~~
> POST /todos HTTP/1.1
> Host: {{<HOST_URL>}}
> User-Agent: rest-client
> X-CHALLENGER: x-challenger-guid
> Content-Type: application/json
> Accept: */*
> Content-Length: 116
| {
| "title": "a title",
| "priority": "extra"
| }
~~~~~~~~

## 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:

```json
{
"errorMessages": [
"Could not find field: priority"
]
}
```





0 comments on commit fdc4e60

Please sign in to comment.