![](/img/trans.png)
[英]How to create a Perl API to pass data back to an angularjs $http request?
[英]how to pass variable to http post API in Perl?
我的摘要如下,我用谷歌搜索,找不到在引用的字符串中將變量傳遞給發布請求的解決方案。 大部分google結果只是將純Json鍵值字符串對傳遞到帖子內容。 但是我需要將參數傳遞給內部Json值部分,並調用相關的REST api。 有什么建議么? 謝謝!
#!/usr/bin/perl -w
use strict;
use warnings;
# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");
# Create a request
my $req = HTTP::Request->new(POST => 'https://oapi.dingtalk.com/robot/send?access_token=foofb73f');
$req->content_type('application/json');
my $var1="value from var";
# works fine
my $message = '{"msgtype": "text","text":{"content":"plain string ok"}}';
# failed to compile
# my $message = '{"msgtype": "text","text":{"content":$var1}}';
$req->content($message);
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print $res->status_line, "n";
}
您沒有將值正確轉換為JSON字符串。
use Cpanel::JSON::XS qw( encode_json );
my $message = encode_json({ msgtype => "text", text => { content => $var1 } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.