When using Losant API- Devices: Patch resource, I used the following JSON Template as request:
{
“deviceIds”: ["{{data.query.deviceId}}"
],
“updateFields”: {
“tags”: [
{
“key”: “group”,
“value”: “Testgroup”
}
]
}
}
This tag updated fine, but the other tags disappeared. Is that the correct behavior?
I actually ran up against this same issue a few weeks ago. What you did is indeed expected behavior because you put the properties to update under the “updateFields” key, which does a full replacement. However, we do also support the simple addition, updating, or removal of tags and attributes - without replacing the existing array - through this same endpoint by putting the instructions under the “updateOperations” tag.
So in your case, your request body would instead look like the following …
{
"deviceIds": ["{{data.query.deviceId}}"],
"updateOperations": [
{ "operation": "set", "target": "tag", "value": { "key": "group", "value": "Testgroup"} }
]
}
I’m going to file a ticket to improve the description and example of this case in our documentation. I agree that it is not clear based on what is there now.
When I try this I get the following error in the api response:
Ah, I assume you’re using an application API token to make this request? If so, you’ll also need to include an email
property …
{
"email": "your@emailaddress.com",
"deviceIds": ["{{data.query.deviceId}}"],
"updateOperations": [
{ "operation": "set", "target": "tag", "value": { "key": "group", "value": "Testgroup"} }
]
}
This won’t happen in your case since you’re only updating a single device, but we require that in case the request kicks off enough device updates that we need to execute the operation asynchronously and then email you a report when it’s done. The reason you don’t have to provide an email address when using the UI is that we can retrieve your email from the user token that authenticates the request.
That worked great. Thanks !
Now my “deviceIds” payload contains multiple devices like this:
Shouldn’t I be able to use the join command so the whole line would be:
“deviceIds”: ["{{join data.query.deviceId}}" ],
?
That will output your device IDs as a single value, like so …
["FIRST_DEVICE_ID,SECOND_DEVICE_ID"]
While that is valid JSON, it is not a valid device ID and thus the request would fail.
Instead, I would use the jsonEncode
helper, like so …
{
"deviceIds": {{jsonEncode data.query.deviceId}}
}
I fixed it by using jsonEncode. Had tried that first but the template tester helped showing the output.
Didn’t realize it output the ‘[’ ‘]’ brackets and everything.