以JSON格式打印http请求响应

问题描述 投票:1回答: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方法正在获取get方法中的所有项目列表。

perl post request get-method
1个回答
0
投票
# Here is the successfully compiled code

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;

$token = $lwp->request( $request ); 

if ( $token->is_success ) {
    my $message = $token->decoded_content;
    print "\n Received POST Response:\n$message\n";
} else {
    print "error code:",    $token->code,"\n";
    print "error message:", $token->res_message, "\n"; 
}
© www.soinside.com 2019 - 2024. All rights reserved.