Perl REST client module - how to return full JSON update

I am using 3rd party REST API to update a custom object. The custom object has a format similar to this:

{
        "id": "abcd1234",
        "profile": {
            "firstName": "Test",
            "lastName": "User",
            "email": "test@somedomain.com",
            ....
        }
}

      

I am making a GET call to get a user object, where $ next is my GET command. I want to update the email attribute and return the entire JSON object. Then I do code like the following to dereference the object and get the email attribute (below). The specific API requires that all profile attributes be specified when updating a user profile. Partial updates are not supported by the api provider. How do I update the value of the email attribute to be something else and return the entire json object? I am looking for the remaining code to help with this.

use JSON qw( decode_json );
use REST::Client;
....

$next = "/api/v1/users?q=Test_User";
$cli->GET($next); 
$json = $cli->responseContent();
my $perl_ref = decode_json($json); #decode the response
foreach my $item( @$perl_ref ) 
{ 
        $email = $item->{'profile'}->{'email'}; #deference email value from user object
        #Update the email attribute value for referenced user object
        #re-encode as JSON, and PUT the entire record to update it

}

      

The JSON that is returned from the GET call looks something like this (below). When I do PUT, I want to update the value of the email attribute, but I only want to DISABLE the profile part and nothing else. How do I change the code for this?

{
    "id": "00u1ujjbx5AZCKALPVHM",
    "passwordChanged": null,
    "profile": {
        "firstName": "John",
        "email": "test@somedomain.com",
    },
    "credentials": {
        "rec_question": {
            "question": "What is your favorite food?"
        },
        "provider": {
            "type": "ACTIVE_DIRECTORY",
            "name": "domain.local"
        }
    },
    "_links": {
        "changeRecoveryQuestion": {
            "href": "abc",
            "method": "POST"
        },
        "deactivate": {
            "href": "def",
            "method": "POST"
        },
        "changePassword": {
            "href": "ghi",
            "method": "POST"
        }
    }
}

      

+3


source to share


1 answer


Just update the referenced object and recode back to json.

#Update the email attribute value for referenced user object
my $my_new_email = "someone@example.com";
$item->{'profile'}->{'email'} = $my_new_email;

#re-encode as JSON, 
my $json_out = encode_json( $item );

#and PUT the entire record to update it
$cli->PUT( $json_out );

      



However, you must check if the API supports PATCH - this allows you to return the fields you want to update without having to do an initial get, you could reduce the above example to:

$cli->PATCH(  '{"id": "abcd1234", "profile":{"email": "someone@example.com"}}  );

      

+1


source







All Articles