Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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::Client模块以编程方式设置POST参数_Perl_Http_Rest_Post - Fatal编程技术网

Perl:使用REST::Client模块以编程方式设置POST参数

Perl:使用REST::Client模块以编程方式设置POST参数,perl,http,rest,post,Perl,Http,Rest,Post,我已经构建了一个REST服务器,现在我想使用REST::Client模块从Perl客户端快速测试它 如果我执行GET请求(在URL中显式设置参数),它可以正常工作,但我不知道如何在POST请求中设置这些参数 我的代码是这样的: #!/usr/bin/perl use strict; use warnings; use REST::Client; my $client = REST::Client->new(); my $request_url = 'http://myHost:66

我已经构建了一个REST服务器,现在我想使用REST::Client模块从Perl客户端快速测试它

如果我执行GET请求(在URL中显式设置参数),它可以正常工作,但我不知道如何在POST请求中设置这些参数

我的代码是这样的:

#!/usr/bin/perl
use strict;
use warnings;

use REST::Client;

my $client = REST::Client->new();

my $request_url =  'http://myHost:6633/my_operation';

$client->POST($request_url); 
print $client->responseContent();
我尝试过类似的方法:

$client->addHeader ('my_param' , 'my value');
但这显然是错误的,因为我不想设置一个HTTP预定义的头,而是一个请求参数

$client->POST('http://localhost:3000/user/0/', '{ "name": "phluks" }', { "Content-type" => 'application/json'});

谢谢大家!

我并没有使用REST模块,但查看,它接受一个body-content参数,尝试创建一个参数字符串并在函数中发送它

$client->POST($request_url, "my_param=my+value"); 
print $client->responseContent();

这很直截了当。但是,您需要知道服务器需要什么类型的内容。这通常是XML或JSON

F.ex。这适用于能够理解第二个参数中JSON的服务器,如果您在第三个参数的头中告诉它JSON是什么

$client->POST('http://localhost:3000/user/0/', '{ "name": "phluks" }', { "Content-type" => 'application/json'});

REST模块接受一个body content参数,但我发现要使其使用一个参数字符串,需要设置一个适当的内容类型

所以下面的代码对我很有用:

$params = $client->buildQuery([username => $args{username},
             password => $args{password}]);

$ret = $client->POST('api/rest/0.001/login', substr($params, 1), 
           {'Content-type' => 'application/x-www-form-urlencoded'});

谢谢你。我只是浪费了一个小时的时间——其余的::客户端文档实际上说使用arrayref调用POST请求作为正文和标题,这是不正确的
POST($url,[$body\u content,%%$headers])
我认为它们的意思是[]是“可选的”,这是标准的unix手册页语言。