PUT and PATCH = Update¶
The PUT and PATCH methods are used to update existing resources, but are used differently:
PUTrequires all the fields in the resource to be provided in the data body, even fields which will not be changing- For example: To update a Location name on an existing Location, you would need to provide the Location
ID,Name,Site,Group, andNotesfields, even if only the Name is changing
- For example: To update a Location name on an existing Location, you would need to provide the Location
PATCHcan perform a partial update on an existing resource. To update the same Location, you could just provide theIDand theNamefields
Both the PUT and PATCH methods work against the API.
The below example will update the name of a Site using the PATCH method.
Modifier Methods
Modification methods like PUT, PATCH, and DELETE are always made against the detail view of a resource (where the ID number of the resource is used in the URL).
Change the name of the Site with ID 1701
$Body = @{
name = "My Newly Named Site"
} | ConvertTo-Json # (1)
Invoke-RestMethod `
-Method 'Patch' `
-Headers @{Authorization="Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} `
-ContentType 'application/json' `
-Body $Body `
-Uri https://deploy.c1engineering.com/api/projects/sites/1701/
- The
ConvertTo-Jsoncmdlet has to be used here due to a bug in Powershell
Change the name of the Site with ID 1701
import json
import requests
response = requests.patch(
'https://deploy.c1engineering.com/api/projects/sites/1701/',
headers={
'Authorization':
'Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
data={
'name': 'My Newly Named Site'
}
)
location = response.json()
print(json.dumps(location, indent=4))