Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl REST客户端模块-如何在更新后放回整个JSON_Perl_Rest - Fatal编程技术网

Perl REST客户端模块-如何在更新后放回整个JSON

Perl REST客户端模块-如何在更新后放回整个JSON,perl,rest,Perl,Rest,我正在使用第三方RESTAPI更新用户对象。用户对象的格式与此类似: { "id": "abcd1234", "profile": { "firstName": "Test", "lastName": "User", "email": "test@somedomain.com", .... } } { "id": "00u1ujjbx5AZCK

我正在使用第三方RESTAPI更新用户对象。用户对象的格式与此类似:

{
        "id": "abcd1234",
        "profile": {
            "firstName": "Test",
            "lastName": "User",
            "email": "test@somedomain.com",
            ....
        }
}
{
    "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"
        }
    }
}
我正在进行GET调用以获取用户对象,其中$next是我的GET命令。我想更新email属性并将整个JSON对象放回原处。然后,我执行如下代码来取消对对象的引用并获得email属性(如下所示)。特定API要求在更新用户配置文件时指定所有配置文件属性。供应商的api不支持部分更新。如何将email属性值更新为其他值,并将整个json对象放回原处?我正在寻找剩余的代码来帮助实现这一点

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

}
{
    "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"
        }
    }
}
GET调用返回的JSON如下所示(如下所示)。当我执行PUT时,我想更新email属性值,但我只想放回profile部分,而不想放其他内容。我需要如何更改代码来实现这一点

{
    "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"
        }
    }
}

只需更新引用对象,然后重新编码回json

{
    "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"
        }
    }
}
#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 );
但是,您应该检查API是否支持修补程序-这允许您发回要更新的字段,而无需执行初始get,您可以将上述示例简化为:

{
    "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"
        }
    }
}
$cli->PATCH(  '{"id": "abcd1234", "profile":{"email": "someone@example.com"}}  );

谢谢你的反馈。你的建议行得通,但我遇到了一个问题。当我进行最初的GET调用时,我得到一些JSON返回给我。我想更新电子邮件属性,它位于返回的JSON的“profile”标记内。我也只想把这部分放回去。我该怎么做?有什么想法吗?谢谢在上面的示例中,将$json\u out更改为我的$json\u out=encode\u json({profile=>$item->{profile});旁注:
“someone@example.com“
应为单引号,
”someone@example.com“
,或将
@
转义为:
“someone\@example.com”
标识符可以展开/插入双引号。