PATCH /profiles/:recipient_id
Unlike a PUT
request, a PATCH
request will update only a portion of a recipient's profile. PATCH
syntax is a little more complicated. Courier uses the JSON Patch syntax described below.
Imagine that we have a profile with an email
and phone_number
. We want to update only the email address.
"profile": {
"email": "normanosborn@oscorp.com",
"phone_number": "555-555-5555",
}
Our patch request body will be JSON. The first key in a JSON Patch object will be op
, which stands for the operation you are to perform. This is assigned a value of add
, remove
, replace
, move
, copy
, or test
. We already have an email address present, so we will use replace
.
We list the value to be replaced by pointing to it using the path
key. This path
takes a value pointing to the key we want to target. The path is built using "/" characters. Our email
key is one level deep, so we will use "/email"
. We then have a value
key that is assigned the value we want to replace our existing email with. The entire line looks like this:
{ "op": "replace", "path": "/email", "value": "test@example.com" }
To send the PATCH
request, we send an array with our JSON Patch objects.
[{ "op": "replace", "path": "/email", "value": "test@example.com" }]
If you want to update multiple values, your patch will look like this:
[
{ "op": "replace", "path": "/email", "value": "test@example.com" },
{ "op": "replace", "path": "/phone_number", "value": "test@example.com" }
]
More Examples
The following types of requests will not change the body of an existing Profile:
// Empty Patch Array
{
"patch": []
}
The above will result with:
{
"status": "SUCCESS"
}
For more details about JSON Patch syntax:
URL: https://api.courier.com/profiles/:recipient_id
Method: PATCH
Path Parameters
Body Parameters
Responses
status: 200 OK
status: 400 Bad Request
Request Example
- cURL
- Node.js
- Ruby
- Python
- Go
- PHP
curl --request PATCH \
--url https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6 \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '
{
"patch": [
{
"op": "replace",
"path": "/email",
"value": "test@example.com"
}
]
}
'
// Dependencies to install:
// $ npm install node-fetch --save
const fetch = require('node-fetch');
const options = {
method: 'PATCH',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"patch": [
{
"op": "replace",
"path": "/email",
"value": "test@example.com"
}
]
})
};
fetch('https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Patch.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request.body = "{\"patch\":[{\"op\":\"replace\",\"path\":\"/email\",\"value\":\"test@example.com\"}]}"
response = http.request(request)
puts response.read_body
# Dependencies to install:
# $ python -m pip install requests
import requests
url = "https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6"
payload = {
"patch": [
{
"op": "replace",
"path": "/email",
"value": "test@example.com"
}
]
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
response = requests.request("PATCH", url, json=payload, headers=headers)
print(response.text)
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6"
payload := strings.NewReader("{\"patch\":[{\"op\":\"replace\",\"path\":\"/email\",\"value\":\"test@example.com\"}]}")
req, _ := http.NewRequest("PATCH", url, payload)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
<?php
// Dependencies to install:
// $ composer require guzzlehttp/guzzle
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('PATCH', 'https://api.courier.com/profiles/0460766e-8463-4905-ae98-b72c7aef41d6', [
'body' => '{"patch":[{"op":"replace","path":"/email","value":"test@example.com"}]}',
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
]);
echo $response->getBody();
Responses Example
{
"status": "SUCCESS"
}
{
"message": "Error Message",
"type": "invalid_request_error"
}