Skip to content

PUT and PATCH = Update

The PUT and PATCH methods are used to update existing resources, but are used differently:

  • PUT requires 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, and Notes fields, even if only the Name is changing
  • PATCH can perform a partial update on an existing resource. To update the same Location, you could just provide the ID and the Name fields

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
curl -s -X PATCH \
    -H "Authorization: Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
    -H "Content-Type: application/json"  \
    -d '{
            "name": "My Newly Named Site"
        }' \
    https://deploy.c1engineering.com/api/projects/sites/1701/
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/
  1. The ConvertTo-Json cmdlet 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))