REST API - Update an Object
Let’s try to perform an update in Xurrent. In this exercise you will rename the ‘Operations’ team in the Widget Data Center Account. Again, you will first lookup the Teams info in the Xurrent REST API section.
Note that in the top right corner of the REST API pages you can find a filter option that allows you to easily find the Teams info.
When you perform an update you need to define the new value(s) in the data (or body) section of the API request. The data must be formatted in the JSON format. This means that you will define the fields that need to be updated as value pairs:
{"field-1":"value1","field-2":"value2"}
When using CURL on a Windows operating system, it is important to escape double quotes in the-d
(i.e. data) option as follows:-d "{\"param\":\"value\"}"
In this exercise you will also use a query string. A query string is part of the URL. In REST API the query string is used to specify parameters to finetune the query for data objects.
The query string appears after a question mark (?) in the endpoint. In the query string, each parameter is listed one right after the other with an ampersand (&) separating them. The order of the query string parameters does not matter.
The Xurrent REST API allows you to filter a list of records with a query string. For example the following CURL statement will return all Configuration Items in Widget Data Center:
curl -X GET -i -H "Authorization: Bearer <personal-token>" -H "X-Xurrent-account: wdc" "https://api.4me-demo.com/v1/cis"
If you only want a list of configuration items with label = ‘CMP00001’, you can add this filter as a query string to the URL as follows:
curl -X GET -i -H "Authorization: Bearer <personal-token>" -H "X-Xurrent-account: wdc" "https://api.4me-demo.com/v1/cis?label=CMP00001"
You can find more information on filtering in Xurrent at this page
You cannot use all characters in an URL: URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. For example spaces are replaced by %20 in HTTP requests.
Exercise:
Can you update the name of the ‘Operations’ team in the ‘wdc’ account to ‘Special Ops’?
Don’t forget to add the record type Team with Read and Update access rights to the personal access token.
As specified here you need the ID of the Team to make an update. First use the Teams API with a filter on Name to find the correct team id:
curl -X GET -i -H "Authorization: Bearer <personal-token>" -H "X-Xurrent-Account: wdc" "https://api.4me-demo.com/v1/teams?name=Operations"
Then use /teams/:id to update the team. Don’t forget to replace the space in the new name by %20. Use the Http verb -X PATCH
to perform the update:
curl -X PATCH -i -H "Authorization: Bearer <personal-token>" -H "X-Xurrent-Account: wdc" -d '{"name":"Special%20Ops"}' "https://api.4me-demo.com/v1/teams/14"