Forum Discussion

Cole_McDonald's avatar
Cole_McDonald
Icon for Professor rankProfessor
5 years ago
Solved

REST API Patch issue...

Has anyone had any success using the v2 of the REST API to push a PATCH to add a customer property to an object (in this case /device/groups/##### without wiping out any other custom properties that have already been set?

We've run into a curious behavior while trying to add a property to feed out integration to our ticketing system.  It seems to replace the whole of the custom properties object, not just the child object within the customproperties level of the json:

{ `"customProperties`" : [{
        `"name`"  : `"connectwisev2.companyid`",
        `"value`" : `"$($integrationID.TrimEnd())`"
    }]
}

We had 2-3 other properties in there that disappeared after patching.

  • Anonymous's avatar
    Anonymous
    5 years ago

    The HTTP verb you use will determine that. If I want to update existing properties, i use PUT. If i want to create a new one, i use POST.

    These examples are for devices, but it's similar for groups:

    To create a new property on device 43 called "my.new.property", I POST to "/device/devices/43/properties/" with the following payload:

    {
        "name": "my.new.property",
        "value": "value of the property"
    }

    If i repeat the same action, i get this response:

    {
        "errorMessage": "Property(name=my.new.property) already exists in device(id=43)",
        "errorCode": 1400,
        "errorDetail": null
    }

     

    So, to update that property with a new value, I do a PUT to "/device/devices/43/properties/my.new.property" with the following payload:

    {"value":"Star Wars!"}

    More reading: https://medium.com/backticks-tildes/restful-api-design-put-vs-patch-4a061aa3ed0b

4 Replies

  • Try using the /device/groups/{gid}/properties/{name}/ endpoint.

  • Anonymous's avatar
    Anonymous

    Yeah, if you patch the group, you'd have to include all children in their entirety. If you patch just one property, you only have to provide that one property in its entirety.

  • Thanks guys.  Will this allow me to add customProperties or am I locked into accessing existing customProperties?

  • Anonymous's avatar
    Anonymous

    The HTTP verb you use will determine that. If I want to update existing properties, i use PUT. If i want to create a new one, i use POST.

    These examples are for devices, but it's similar for groups:

    To create a new property on device 43 called "my.new.property", I POST to "/device/devices/43/properties/" with the following payload:

    {
        "name": "my.new.property",
        "value": "value of the property"
    }

    If i repeat the same action, i get this response:

    {
        "errorMessage": "Property(name=my.new.property) already exists in device(id=43)",
        "errorCode": 1400,
        "errorDetail": null
    }

     

    So, to update that property with a new value, I do a PUT to "/device/devices/43/properties/my.new.property" with the following payload:

    {"value":"Star Wars!"}

    More reading: https://medium.com/backticks-tildes/restful-api-design-put-vs-patch-4a061aa3ed0b