使用Perl Client的Ruby rest Sinatra服务器

时间:2017-08-02 17:42:26

标签: perl rest sinatra

我的Ruby服务器

require 'sinatra'
set :port, 8080
get '/get' do
  "GET Hello World"
end

post '/post' do
  "POST Hello World"
end

Ruby客户端 - 工作

require "rest-client"
response = RestClient.post 'http://localhost:8080/post', {param1: 'one'}
puts response

Curl Works

curl -v -X POST http://localhost:8080/post

如果我通过浏览器调用URL,它也能正常工作。

我的Perl客户端不适用于此服务器。我知道这是一个Perl问题,但适用于其他服务器。两个perl脚本都不起作用

use REST::Client;
my $client = REST::Client->new();
$client->POST('http://localhost:8080/post');  #Updated to post
print $client->responseContent();

8月3日增加:上述

的输出
Can\'t connect to localhost:8080
Connection refused at /usr/local/share/perl5/LWP/Protocol/http.pm line 46.

use LWP::UserAgent;
use HTTP::Request;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( POST => "http://localhost:8080/post"); #Updated
my $response = $ua->request($req);
print $response->status_line . "\n"; #Updated 
print $response->content;

欣赏任何指示。

更新:

在我的代码中出现GET / POST错误后,它开始在一台服务器上运行。但是在另一台服务器上,我正在

Can't connect to localhost:8080 (Connection refused)

LWP::Protocol::http::Socket: connect: Connection refused at /usr/local/share/perl5/LWP/Protocol/http.pm line 46.

这与IPc4&有关吗? IPv6的?

0 个答案:

没有答案