Middle
REST API task: creating an order in a bookstore — method, request body, response, idempotency.
sobes.tech AI
Answer from AI
To create an order in a bookstore via REST API, the POST method is usually used, as a new resource — the order — is created.
Request body contains the order data, for example:
{
"customerId": "12345",
"items": [
{"bookId": "987", "quantity": 2},
{"bookId": "654", "quantity": 1}
],
"paymentMethod": "credit_card",
"shippingAddress": "Lenin St., 10"
}
Response upon successful creation usually contains status 201 Created and a body with information about the created order, including a unique identifier:
{
"orderId": "abc123",
"status": "created",
"createdAt": "2024-06-01T12:00:00Z"
}
Idempotency:
POST requests by default are not idempotent, i.e., a repeated call will create a new order. To ensure idempotency, a unique client identifier (for example, Idempotency-Key in headers) can be used, which the server will check and will not create a duplicate order upon repeated requests with the same key.