Reputation: 2700
I am designing a RESTful API to represent orders and payment transactions. My (simplified) model looks like this:
--------- ---------------
| order |----<| transaction |
--------- ---------------
i.e. an order can have many transactions. If I have these endpoints to GET the resources:
/orders/{id}
/orders/{id}/transactions
Where the second call returns a list of transaction objects (expanded or just ids that can be accessed via:)
/transactions/{id}
Then how should I update the order resource to describe its transactions?
Should I POST a list of transaction object data to /orders/{id}/transactions
and they will be created and linked to the order resource?
Or should I POST to /transactions
each transaction and then PUT (or POST?) to /orders/{id}/transactions
a list of transaction IDs?
...or something else?
What if I already have a transaction resource and want to link it?
Upvotes: 0
Views: 234
Reputation: 130887
You could use POST
to create a transaction for an order, as following:
POST /orders/{id}/transactions HTTP/1.1
Host: example.org
Content-Type: application/json
{
... transaction representation ...
}
To link an existing transaction to an order you could consider the transaction resource contains a sub-resource called order. To replace the order of a transaction, you would use PUT
, sending the order identifier in the request payload:
PUT /transactions/{id}/order HTTP/1.1
Host: example.org
Content-Type: application/json
{
"order": 100
}
Alternatively, you could use PATCH
and JSON Patch to partially update the transaction resource:
PATCH /transactions/{id} HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json
[
{ "op": "replace", "path": "/order", "value": 100 }
]
Upvotes: 3