1
votes
#!/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";
}

Please help me to get output with JSON format of ie requesting method with HTTP req and get method is capturing with all the projects list in getting method.

1
Is this your own code?Borodin
GET request messages don't have a Content-Type header.Borodin
Please make your code compile and re-post it.Borodin
You're not getting any output from that code. Fix the errors so that it compiles and re-post it.Borodin
Your question doesn't clearly identify your problem. What output are you getting? What about are you expecting? Why are you expecting that? Honestly, though, there's not much we can do if you get the wrong output from a script we haven't seen!ikegami

1 Answers

0
votes
# 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"; 
}