调用Perl的Mojolicious来调用REST服务时解码响应体的问题。

huangapple go评论53阅读模式
英文:

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 = &quot;http://localhost:31297/api/CallBack/RegisterCallBackURL?&quot;;
$requestURL = $requestURL.&quot;CallBackURL=http://localhost:8081/status&quot;;
$requestURL = $requestURL.&quot;&amp;VerboseFlag=false&quot;;
$requestURL = $requestURL.&quot;&amp;api-version=1.2&quot;;
my $Acknowledged = &quot;&quot;;
my $VerboseInfo = &quot;&quot;;
my $CallBackURL = &quot;&quot;;
my $RequestID = &quot;&quot;;
my $CurrentJobStatus = &quot;&quot;;
my $TimeStampReceived = &quot;&quot;;
my $ua = Mojo::UserAgent-&gt;new;
my $tx = $ua-&gt;post($requestURL,
                form =&gt; {
                &quot;Acknowledged&quot; =&gt; $Acknowledged,
                &quot;VerboseInfo&quot; =&gt; $VerboseInfo,
		&quot;CallBackURL&quot; =&gt; $CallBackURL,
	        &quot;RequestID&quot; =&gt; $RequestID,
	         &quot;CurrentJobStatus&quot; =&gt; $CurrentJobStatus,
	         &quot;TimeStampReceived&quot; =&gt; $TimeStampReceived
                   });
my $res = $tx-&gt;result;				   	
if ($res-&gt;is_success) 
{
	print &quot;success&quot;;
	print $res-&gt;message;
	print $res-&gt;body;
	print $RequestID;
}
else
{
	print &quot;Error&quot;;
	print $res-&gt;message;
}

I get back the following response, but the individual variables do not contain data.

Process started (PID=5440) &gt;&gt;&gt;
successOK{&quot;Acknowledged&quot;:false,&quot;VerboseInfo&quot;:false,&quot;CallBackURL&quot;:&quot;http://localhost:8081/status&quot;,&quot;RequestID&quot;:&quot;98d8df79-6897-4716-8084-ee756d16a1bf&quot;,&quot;CurrentJobStatus&quot;:null,&quot;TimeStampReceived&quot;:&quot;2023-06-29T04:44:03.2440429-07:00&quot;}&lt;&lt;&lt; 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-&gt;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 = &quot;http://localhost:31297/api/CallBack/RegisterCallBackURL&quot;;
my $form = {
	CallBackURL =&gt; &#39;http://localhost:8081/status&#39;,
	...
	Acknowledged =&gt; $Acknowledged,
	...
	};

my $tx = $ua-&gt;post( $url =&gt; form =&gt; $form );
if( $tx-&gt;res-&gt;is_success ) {
	my $data = $tx-&gt;res-&gt;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 = &#39;http://localhost:31297/api/CallBack/RegisterCallBackURL&#39;;
my $url = Mojo::URL-&gt;new( $base )-&gt;query(
	CallBackURL   =&gt; &#39;http://localhost:8081/status&#39;,
	VerboseFlag   =&gt; &#39;false&#39;,
	&#39;api-version&#39; =&gt; &#39;1.2&#39;,
	);

print $url;

I have a lot more examples in my book Mojolicious Web UserAgents.

huangapple
  • 本文由 发表于 2023年6月29日 20:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定