Partially update a book with a JSON merge patch
PATCH
https://api.testingapis.com/api/Books/{id}Only the fields present in the body change, and nested objects are merged rather than replaced. The ID and unknown fields are never applied; they are listed in ignored. The patched record is validated before it is saved, so a rejected patch changes nothing.
Parameters 1
| Name | Type | Description |
|---|---|---|
idrequired | path · integer (int32) | Record ID. |
Request body
Optional. application/json, text/json, application/*+json, application/xml, text/xml, application/*+xml. JSON object with the fields to change, e.g. { "price": 12.5 }.
Example body
{"title": "Clean Code"}Responses
- 200
- Patched; returns { message, changed, ignored, data }.
- 400
- The body is not an object, sets a non-nullable field to null, has a wrongly typed value, or the result fails validation.
- 404
- No record has this ID.
Example
Request
curl -X PATCH "https://api.testingapis.com/api/Books/26" \
-H "Content-Type: application/json" \
-d '{"title": "Clean Code"}'Response200 OK
{
"message": "Book patched successfully",
"changed": ["title"],
"ignored": [],
"data": {
"id": 26,
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "9780132350884",
"genre": "programming",
"publishedYear": 2008,
"pages": 464,
"language": "English",
"price": 37.99,
… (5 more lines)Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/Books/26", {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: "{\"title\": \"Clean Code\"}",
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"PATCH",
"https://api.testingapis.com/api/Books/26",
headers={
"Content-Type": "application/json",
},
data="{\"title\": \"Clean Code\"}".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/Books/26"))
.header("Content-Type", "application/json")
.method("PATCH", HttpRequest.BodyPublishers.ofString("{\"title\": \"Clean Code\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}