1
#!/usr/bin/perl

use strict;
use warnings;

use JSON qw(decode_json);
use JSON;
use LWP::UserAgent;
use HTTP::Request;

#GET_METHOD
my $usagnt_get           = LWP::UserAgent->new;
my $server_end_point_get = "http://192.168.201.1:8000/c/r";
my $reqst_get            = HTTP::Request->new( GET => $server_endpoint_get );
$reqst_get->header( 'content-type' => 'application/json' );

#Request User Agent
my $respnse_get = $usagnt->request( $reqst_get );

if ( $resp_get->is_success ) {
    my $message = $respnse_get->decoded_content;
    print "\n Received GET Response:\n$res_message\n";
    print "\n****** GET operations SUCCESS..!\n";
}
else {
    print "HTTP_GET error code:",    $respnse_get->code,        "\n";
    print "HTTP_GET error message:", $respnse_get->res_message, "\n";
}

请帮助我以 JSON 格式获取输出,即使用 HTTP req 请求方法,get 方法正在捕获方法中的所有项目列表。

4

1 回答 1

0
# Here is the successfully compiled code


!/usr/bin/perl

use strict;
use LWP::UserAgent; 

my $token="";

my $uri = 'http://xxx.xxx.xxx.xxx:8000/a/b'; 
my $json => '{"username":"user","password":"pwd"}'; 
my $req = HTTP::Request->new('POST', $uri ); 
$request->header( 'Content-Type' => 'application/json'); 
$request->content( $json );

my $lwp = LWP::UserAgent->new;

my $message = $lwp->request( $request ); 

if ( $message->is_success ) {
    my $token= $message->content;
    print "\n Received POST Response:\n$token\n";
} else {
    print "error code:",    $message->code,"\n";
    print "error message:", $message->as_string(), "\n"; 
}
于 2020-02-11T07:47:31.393 回答