Validate a DataAnnotations model using the framework's automatic 400
POST
https://api.testingapis.com/api/validation/annotationsInvalid models are rejected by [ApiController] before the action runs, with a standard ValidationProblemDetails body.
Request body
Optional. application/json, text/json, application/*+json, application/xml, text/xml, application/*+xml. Model: ValidationControllerAnnotatedSignup. The signup to validate.
Example body
{
"username": "ava_t",
"email": "ava@example.com",
"age": 29,
"countryCode": "US",
"website": "https://ava.dev",
"phone": "+1 555 0101",
"tags": ["qa", "api"]
}Responses
- 200
- OK
- 201
- Model is valid.
- 400
- Validation errors (ValidationProblemDetails).
Example
Request
curl -X POST "https://api.testingapis.com/api/validation/annotations" \
-H "Content-Type: application/json" \
-d '{"username": "ava_t", "email": "ava@example.com", "age": 29, "countryCode": "US", "website": "https://ava.dev", "phone": "+1 555 0101", "tags": ["qa", "api"]}'Response201 Created
{
"message": "Signup is valid",
"data": {
"username": "ava_t",
"email": "ava@example.com",
"age": 29,
"countryCode": "US",
"website": "https://ava.dev",
"phone": "+1 555 0101",
"tags": ["qa", "api"]
}
}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/validation/annotations", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "{\"username\": \"ava_t\", \"email\": \"ava@example.com\", \"age\": 29, \"countryCode\": \"US\", \"website\": \"https://ava.dev\", \"phone\": \"+1 555 0101\", \"tags\": [\"qa\", \"api\"]}",
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"POST",
"https://api.testingapis.com/api/validation/annotations",
headers={
"Content-Type": "application/json",
},
data="{\"username\": \"ava_t\", \"email\": \"ava@example.com\", \"age\": 29, \"countryCode\": \"US\", \"website\": \"https://ava.dev\", \"phone\": \"+1 555 0101\", \"tags\": [\"qa\", \"api\"]}".encode("utf-8"),
allow_redirects=False,
)
print(response.status_code, response.text)Java (HttpClient)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.testingapis.com/api/validation/annotations"))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"username\": \"ava_t\", \"email\": \"ava@example.com\", \"age\": 29, \"countryCode\": \"US\", \"website\": \"https://ava.dev\", \"phone\": \"+1 555 0101\", \"tags\": [\"qa\", \"api\"]}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}