Updating an existing JSON API Resource is done by calling the edit
action of your API with:
HTTP PATCH
request typeAccept
header set to application/vnd.api+json
Content-Type
header set to application/vnd.api+json
id
of the resource to updateA successful request will respond with HTTP response code 200
and response body similar to the one produced by the view
action.
A valid JSON API document structure for updating the name
field
for a Country with id
10 would look similar to the following output
produced by http://example.com/countries/1
:
{
"data": {
"type": "countries",
"id": "10",
"attributes": {
"name": "My new name"
}
}
}
When updating a primary JSON API Resource, you can use the same PATCH request to set one or multiple To-One
(or belongsTo
) relationships but only as long as the following conditions are met:
id
of the related resource MUST correspond with an EXISTING foreign keyFor example, a valid JSON API document structure that would set a single related
national-capital
for a given country
would look like:
{
"data": {
"type": "countries",
"id": "2",
"relationships": {
"national-capital": {
"data": {
"type": "national-capitals",
"id": "4"
}
}
}
}
}
Note
Please note that JSON API does not support updating attributes for the related resource(s) and thus will simply ignore them if detected in the request body.
When updating a primary JSON API Resource, you can use the same PATCH request to set one or multiple To-Many
(or hasMany
) relationships but only as long as the following conditions are met:
id
of the related resource MUST correspond with an EXISTING foreign keyFor example, a valid JSON API document structure that would set multiple related cultures
for a given country
would look like:
{
"data": {
"type": "countries",
"id": "2",
"relationships": {
"cultures": {
"data": [
{
"type": "cultures",
"id": "2"
},
{
"type": "cultures",
"id": "3"
}
]
}
}
}
}
Note
Please note that JSON API does not support updating attributes for the related resource(s) and thus will simply ignore them if detected in the request body.