简体   繁体   English

Perl等效于示例JavaScript代码

[英]Perl equivalent of a sample JavaScript codes

I have this code: 我有以下代码:

  request({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: PAGE_ACCESS_TOKEN },
    method: 'POST',
    json: messageData
  })

I'd like to convert that into Perl, what I have so far is: 我想将其转换为Perl,到目前为止,我得到的是:

my $req = HTTP::Request->new( 'POST', 'https://graph.facebook.com/v2.6/me/messages');
$req->header( 'Content-Type' => 'application/json' );
$req->content( $messageData );

I not sure how I can incorporate the following line into my Perl code: 我不确定如何将以下行合并到我的Perl代码中:

qs: { access_token: PAGE_ACCESS_TOKEN },

It specifies a query parameter to add to the URL. 它指定要添加到URL的查询参数。

I tried to search the net but most example either sends the json content or the query string but not both. 我尝试搜索网络,但大多数示例要么发送json内容,要么发送查询字符串,但不能同时发送。 I need something that can send both if my interpretation to the JavaScript code is correct. 如果我对JavaScript代码的解释正确,那么我需要可以同时发送这两种内容的东西。

Thanks in advance to anyone who will guide me. 在此先感谢任何引导我的人。

You can use the URI module (possibly supplemented by the URI::QueryParam module) to build (and manipulate) a URL. 您可以使用URI模块(可能由URI :: QueryParam模块补充)来构建(和操作)URL。

use HTTP::Request::Common qw( POST );
use JSON::XS              qw( encode_json );
use URI                   qw( );

my $message_data = encode_json(...);

my $url = URI->new('https://graph.facebook.com/v2.6/me/messages');
$url->query_form( access_token => PAGE_ACCESS_TOKEN );

my $req = POST($url,
   Content_Type => 'application/json',
   Content      => $message_data,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM