Update
Examples in this documentation depend on features activated in the The code samples demonstrate how to contact the server over HTTPS using the deployment CA certificate. Before trying the samples, generate the CA certificate in PEM format from the server deployment ID and password:
|
Update a resource
Use HTTP PUT to replace any and all writable fields in a resource.
The effect is the same as a patch replace operation.
The following example updates Sam Carter’s telephone number regardless of the revision:
-
Curl
-
JavaScript
-
Python
-
Ruby
$ curl \
--request PUT \
--cacert ca-cert.pem \
--user dc=com/dc=example/ou=People/uid=kvaughan:bribery \
--header 'Content-Type: application/json' \
--data '{"telephoneNumber": "+1 408 555 1212"}' \
'https://localhost:8443/hdap/dc=com/dc=example/ou=People/uid=scarter?_fields=telephoneNumber&_prettyPrint=true'
{
"_id" : "dc=com/dc=example/ou=People/uid=scarter",
"telephoneNumber" : [ "+1 408 555 1212" ]
}
(async () => {
const { authenticate, doRequest, getOptions } = require('./utils')
const options = getOptions({
path: '/hdap/dc=com/dc=example/ou=People/uid=scarter?_fields=telephoneNumber',
method: 'PUT',
body: { "telephoneNumber": "+1 408 555 1212" }
})
const jwt = await authenticate(options)
options.headers['Authorization'] = 'Bearer ' + jwt
const response = await doRequest('HDAP: update telephone number', options)
console.log(response)
})().catch(error => { console.error(error) })
#!/usr/bin/env python3
import requests
import utils
body = { 'telephoneNumber': '+1 408 555 1212' }
jwt = utils.authenticate('dc=com/dc=example/ou=People/uid=kvaughan', 'bribery')
headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {jwt}' }
params = { '_fields': 'telephoneNumber' }
response = requests.put(
f'https://{utils.host}:{utils.port}/hdap/dc=com/dc=example/ou=People/uid=scarter',
headers=headers,
json=body,
params=params,
verify=utils.ca_pem)
print('Status code: %d\nJSON: %s' % (response.status_code, response.json()))
require_relative 'utils.rb'
require 'faraday'
require 'json'
utils = Utils.new('dc=com/dc=example/ou=People/uid=kvaughan', 'bribery')
options = { ca_file: utils.ca_pem }
jwt = utils.authenticate
fields = { '_fields': 'telephoneNumber' }
hdap = Faraday.new(url: "https://#{utils.host}:#{utils.port}/hdap/", params: fields, ssl: options) do |f|
f.headers['Content-Type'] = 'application/json'
f.request :authorization, 'Bearer', jwt
end
body = { "telephoneNumber" => "+1 408 555 1212" }
response = hdap.put do |h|
h.path = 'dc=com/dc=example/ou=People/uid=scarter'
h.body = JSON.generate(body)
end
puts "Status code: #{response.status}\nJSON: #{response.body}"
HDAP Ruby examples require Ruby 3.2 and the faraday
and json
gems.
Update a specific revision
To update a resource only if the resource matches a particular version,
use an If-Match: <revision>
header:
-
Curl
-
JavaScript
-
Python
-
Ruby
$ export JWT=$(echo $(curl \
--request POST \
--cacert ca-cert.pem \
--header 'Content-Type: application/json' \
--data '{ "password": "bribery" }' \
--silent \
'https://localhost:8443/hdap/dc=com/dc=example/ou=People/uid=kvaughan?_action=authenticate') | jq -r .access_token)
$ export REVISION=$(cut -d \" -f 8 <(curl \
--get \
--cacert ca-cert.pem \
--header "Authorization: Bearer $JWT" \
--header 'Content-Type: application/json' \
--data '_fields=_rev' \
--silent \
'https://localhost:8443/hdap/dc=com/dc=example/ou=People/uid=scarter'))
$ curl \
--request PUT \
--cacert ca-cert.pem \
--header "Authorization: Bearer $JWT" \
--header 'Content-Type: application/json' \
--header "If-Match: $REVISION" \
--data '{"telephoneNumber": "+1 408 555 1213"}' \
'https://localhost:8443/hdap/dc=com/dc=example/ou=People/uid=scarter?_fields=telephoneNumber&_prettyPrint=true'
{
"_id" : "dc=com/dc=example/ou=People/uid=scarter",
"telephoneNumber" : [ "+1 408 555 1213" ]
}
(async () => {
const { authenticate, doRequest, getOptions } = require('./utils')
const options = getOptions({
path: '/hdap/dc=com/dc=example/ou=People/uid=scarter?_fields=telephoneNumber',
body: { "telephoneNumber": "+1 408 555 1213" }
})
const jwt = await authenticate(options)
options.headers['Authorization'] = 'Bearer ' + jwt
let response = await doRequest('HDAP: read scarter _rev', options)
console.log(response)
options.headers['If-Match'] = JSON.parse(response.data)._rev
options.method = 'PUT'
response = await doRequest('HDAP: update specific revision', options)
console.log(response)
})().catch(error => { console.error(error) })
Source files for this sample: update-rev.js, utils.js
#!/usr/bin/env python3
import requests
import utils
jwt = utils.authenticate('dc=com/dc=example/ou=People/uid=kvaughan', 'bribery')
headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {jwt}' }
rev = requests.get(f'https://{utils.host}:{utils.port}/hdap/dc=com/dc=example/ou=People/uid=scarter',
headers=headers,
verify=utils.ca_pem).json()['_rev']
headers['If-Match'] = rev
body = { 'telephoneNumber': '+1 408 555 1213' }
params = { '_fields': 'telephoneNumber' }
response = requests.put(
f'https://{utils.host}:{utils.port}/hdap/dc=com/dc=example/ou=People/uid=scarter',
headers=headers,
json=body,
params=params,
verify=utils.ca_pem)
print('Status code: %d\nJSON: %s' % (response.status_code, response.json()))
Source files for this sample: utils.py, update-rev.py
require_relative 'utils.rb'
require 'faraday'
require 'json'
utils = Utils.new('dc=com/dc=example/ou=People/uid=kvaughan', 'bribery')
options = { ca_file: utils.ca_pem }
jwt = utils.authenticate
fields = { '_fields': 'telephoneNumber' }
hdap = Faraday.new(url: "https://#{utils.host}:#{utils.port}/hdap/", params: fields, ssl: options) do |f|
f.headers['Content-Type'] = 'application/json'
f.request :authorization, 'Bearer', jwt
end
resource = 'dc=com/dc=example/ou=People/uid=scarter'
rev = JSON.parse(hdap.get(resource).body, symbolize_names: true)[:_rev]
body = { "telephoneNumber" => "+1 408 555 1213" }
response = hdap.put do |h|
h.path = resource
h.body = JSON.generate(body)
h.headers['If-Match'] = rev
end
puts "Status code: #{response.status}\nJSON: #{response.body}"
Source files for this sample: utils.rb, update-rev.rb
HDAP Ruby examples require Ruby 3.2 and the faraday
and json
gems.
Rename a resource
Refer to the rename action.