英文:
Problems decoding the response body when calling a REST service with Mojolicious for Perl
问题
我正在尝试学习如何使用Perl的Mojolicious框架。
我正在尝试向一个REST服务注册回调URL并保存REST服务返回的RequestID。我成功地获得了请求,但无法解码哈希的各个值。
我有以下代码:
use Mojo::UserAgent;
use Mojo::Message::Response;
use Mojo::JSON;
use Mojo::Promise;
use Mojo::Content::MultiPart;
my $requestURL = "http://localhost:31297/api/CallBack/RegisterCallBackURL?";
$requestURL = $requestURL."CallBackURL=http://localhost:8081/status";
$requestURL = $requestURL."&VerboseFlag=false";
$requestURL = $requestURL."&api-version=1.2";
my $Acknowledged = "";
my $VerboseInfo = "";
my $CallBackURL = "";
my $RequestID = "";
my $CurrentJobStatus = "";
my $TimeStampReceived = "";
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post($requestURL,
form => {
"Acknowledged" => $Acknowledged,
"VerboseInfo" => $VerboseInfo,
"CallBackURL" => $CallBackURL,
"RequestID" => $RequestID,
"CurrentJobStatus" => $CurrentJobStatus,
"TimeStampReceived" => $TimeStampReceived
});
my $res = $tx->result;
if ($res->is_success)
{
print "success";
print $res->message;
print $res->body;
print $RequestID;
}
else
{
print "Error";
print $res->message;
}
我得到以下响应,但各个变量不包含数据。
Process started (PID=5440) >>>
successOK{"Acknowledged":false,"VerboseInfo":false,"CallBackURL":"http://localhost:8081/status","RequestID":"98d8df79-6897-4716-8084-ee756d16a1bf","CurrentJobStatus":null,"TimeStampReceived":"2023-06-29T04:44:03.2440429-07:00"}<<< Process finished (PID=5440). (Exit code 0)
================ READY ================
英文:
I am trying to learn how to use the Mojolicious framework for Perl.
I am attempting to register a callback URL with a REST service and save the RequestID returned by the REST service. I successfully get back the request but can't decode the individual values that are part of the hash.
I have the following code:
use Mojo::UserAgent;
use Mojo::Message::Response;
use Mojo::JSON;
use Mojo::Promise;
use Mojo::Content::MultiPart;
my $requestURL = "http://localhost:31297/api/CallBack/RegisterCallBackURL?";
$requestURL = $requestURL."CallBackURL=http://localhost:8081/status";
$requestURL = $requestURL."&VerboseFlag=false";
$requestURL = $requestURL."&api-version=1.2";
my $Acknowledged = "";
my $VerboseInfo = "";
my $CallBackURL = "";
my $RequestID = "";
my $CurrentJobStatus = "";
my $TimeStampReceived = "";
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post($requestURL,
form => {
"Acknowledged" => $Acknowledged,
"VerboseInfo" => $VerboseInfo,
"CallBackURL" => $CallBackURL,
"RequestID" => $RequestID,
"CurrentJobStatus" => $CurrentJobStatus,
"TimeStampReceived" => $TimeStampReceived
});
my $res = $tx->result;
if ($res->is_success)
{
print "success";
print $res->message;
print $res->body;
print $RequestID;
}
else
{
print "Error";
print $res->message;
}
I get back the following response, but the individual variables do not contain data.
Process started (PID=5440) >>>
successOK{"Acknowledged":false,"VerboseInfo":false,"CallBackURL":"http://localhost:8081/status","RequestID":"98d8df79-6897-4716-8084-ee756d16a1bf","CurrentJobStatus":null,"TimeStampReceived":"2023-06-29T04:44:03.2440429-07:00"}<<< Process finished (PID=5440). (Exit code 0)
================ READY ================
答案1
得分: 2
你已收到一个JSON响应(可以在$res->body
中看到)。您需要解码该响应(使用Mojo::JSON),然后将各个数据项提取到您的变量中。
英文:
You have received a JSON response (which you can see in $res->body
). You need to decode that response (using Mojo::JSON) and then extract the individual data items into your variables.
答案2
得分: 1
你有很多事情要处理,其中大部分你可以简化。大多数情况下,你不需要加载任何你没有明确使用的模块。Mojolicious 将加载它所需的一切,你只需遵循接口。
也许你有一些奇怪的应用程序,将 URL 参数与正文参数区分对待,但对于许多情况,它们应该一起使用。
之后,你有一个 JSON 响应。使用 json
响应方法将其转换为 Perl 数据结构:
use Mojo::UserAgent;
my $url = "http://localhost:31297/api/CallBack/RegisterCallBackURL";
my $form = {
CallBackURL => 'http://localhost:8081/status',
...
Acknowledged => $Acknowledged,
...
};
my $tx = $ua->post( $url => form => $form );
if( $tx->res->is_success ) {
my $data = $tx->res->json;
... 现在可以处理数据了 ...
}
但让我们回到 URL 中的查询参数。这仍然非常奇怪(但在某些地方存在)。如果你想这样做,而且已经在使用 Mojolicious,可以使用 Mojo::URL 来组合它:
use Mojo::URL;
my $base = 'http://localhost:31297/api/CallBack/RegisterCallBackURL';
my $url = Mojo::URL->new( $base )->query(
CallBackURL => 'http://localhost:8081/status',
VerboseFlag => 'false',
'api-version' => '1.2',
);
print $url;
我在我的书 Mojolicious Web UserAgents 中有更多示例。
英文:
You've got a lot going on there, most of which you can simply. Most of the time, you don't need to load any modules that you don't explicitly use. Mojolicious is going to load everything it needs and you just follow the interface.
Maybe you have some sort of weird app that treats URL params differently from body params, but for many cases that should go together.
After that, you have a JSON response. Use the json
response method to turn that into a Perl data structure:
use Mojo::UserAgent;
my $url = "http://localhost:31297/api/CallBack/RegisterCallBackURL";
my $form = {
CallBackURL => 'http://localhost:8081/status',
...
Acknowledged => $Acknowledged,
...
};
my $tx = $ua->post( $url => form => $form );
if( $tx->res->is_success ) {
my $data = $tx->res->json;
... now do stuff with data ...
}
But let's back up to the query params in the URL. That's still really weird (but exists some places). If you want to do that and you are already using Mojolicious, use Mojo::URL to put it together:
use Mojo::URL;
my $base = 'http://localhost:31297/api/CallBack/RegisterCallBackURL';
my $url = Mojo::URL->new( $base )->query(
CallBackURL => 'http://localhost:8081/status',
VerboseFlag => 'false',
'api-version' => '1.2',
);
print $url;
I have a lot more examples in my book Mojolicious Web UserAgents.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论