Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl REST Client Module - How to put back entire JSON afer update

Tags:

rest

perl

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

{
        "id": "abcd1234",
        "profile": {
            "firstName": "Test",
            "lastName": "User",
            "email": "[email protected]",
            ....
        }
}

I am making a GET call to get the user object where $next is my GET command. I want to update the email attribute and put the entire JSON object back. I then do some code like the following to dereference the object and get the email attribute (below). The specific API requires that all profile attributes to be specified when updating a user’s profile. Partial updates are not supported by the vendor's api. How do I update the email attribute value to be something else, and put the entire json object back? I'm looking for the remaining code to help accomplish 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 gets returned from the GET call looks something like this (below). When I do the PUT, I want to update the email attribute value, but I only want to PUT back the profile part and nothing else. How do I need to change my code to achieve this?

{
    "id": "00u1ujjbx5AZCKALPVHM",
    "passwordChanged": null,
    "profile": {
        "firstName": "John",
        "email": "[email protected]",
    },
    "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"
        }
    }
}
like image 672
user2306111 Avatar asked Jan 24 '26 07:01

user2306111


1 Answers

Simply update the reference object, and re-encode back to json.

#Update the email attribute value for referenced user object
my $my_new_email = "[email protected]";
$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 should check to see if the API supports PATCH - this allows you to send back the fields you want updated, without having to do an initial get, you could reduce the above example to:

$cli->PATCH(  '{"id": "abcd1234", "profile":{"email": "[email protected]"}}  );
like image 77
harvey Avatar answered Jan 25 '26 22:01

harvey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!