Update a cart
PUT
https://api.testingapis.com/api/Carts/{id}Use this to add or remove items.
Parameters 1
| Name | Type | Description |
|---|---|---|
idrequired | path · integer (int32) | Cart ID. |
Request body
Optional. application/json, text/json, application/*+json, application/xml, text/xml, application/*+xml. Model: Cart. The new field values.
Example body
{
"userId": 101,
"items": [
{"productId": 3, "quantity": 2, "price": 89.99},
{"productId": 7, "quantity": 1, "price": 49.99}
],
"total": 229.97,
"updatedAt": "2025-07-16T12:00:00Z"
}Responses
- 200
- Updated; returns { message, data }.
- 404
- The cart does not exist.
- 500
- Unexpected server error.
Example
Request
curl -X PUT "https://api.testingapis.com/api/Carts/9" \
-H "Content-Type: application/json" \
-d '{"userId": 101, "items": [{"productId": 3, "quantity": 2, "price": 89.99}, {"productId": 7, "quantity": 1, "price": 49.99}], "total": 229.97, "updatedAt": "2025-07-16T12:00:00Z"}'Response200 OK
{
"message": "Cart updated successfully",
"data": {
"id": 9,
"userId": 101,
"items": [
{"productId": 3, "quantity": 2, "price": 89.99},
{"productId": 7, "quantity": 1, "price": 49.99}
],
"total": 229.97,
"updatedAt": "2026-09-30T18:26:45Z"
}
}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/Carts/9", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: "{\"userId\": 101, \"items\": [{\"productId\": 3, \"quantity\": 2, \"price\": 89.99}, {\"productId\": 7, \"quantity\": 1, \"price\": 49.99}], \"total\": 229.97, \"updatedAt\": \"2025-07-16T12:00:00Z\"}",
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"PUT",
"https://api.testingapis.com/api/Carts/9",
headers={
"Content-Type": "application/json",
},
data="{\"userId\": 101, \"items\": [{\"productId\": 3, \"quantity\": 2, \"price\": 89.99}, {\"productId\": 7, \"quantity\": 1, \"price\": 49.99}], \"total\": 229.97, \"updatedAt\": \"2025-07-16T12:00:00Z\"}".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/Carts/9"))
.header("Content-Type", "application/json")
.method("PUT", HttpRequest.BodyPublishers.ofString("{\"userId\": 101, \"items\": [{\"productId\": 3, \"quantity\": 2, \"price\": 89.99}, {\"productId\": 7, \"quantity\": 1, \"price\": 49.99}], \"total\": 229.97, \"updatedAt\": \"2025-07-16T12:00:00Z\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}