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";
}

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.

Panciz
  • 2,183
  • 2
  • 30
  • 54
Kumar
  • 21
  • 3
  • 2
    Is this your own code? – Borodin Jul 19 '17 at 12:28
  • 3
    `GET` request messages don't have a `Content-Type` header. – Borodin Jul 19 '17 at 12:29
  • 3
    Please make your code compile and re-post it. – Borodin Jul 19 '17 at 12:30
  • ok thanks for your solution.but i'm getting output as BUt i want in Json output Received POST ID : {"id":"something"} ##################successfully posted Json############### Received GET Response : [{"pI":"somthing","state":"DRAFT","rootApplicationId":"ca985299-41b3-449d-aa51-4284866eb762","properties":{"title":"SET Forms"},"createdDate":1500372075286,"createdBy":null,"updatedDate":null,"updatedBy":null,"applications":[{"applicationId":"7ba762cf-91bb-4c86-b759-c9245c81826d","state":"DRAFT" – Kumar Jul 19 '17 at 13:18
  • 2
    You're not getting any output from that code. Fix the errors so that it compiles and re-post it. – Borodin Jul 19 '17 at 13:58
  • 2
    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 Jul 19 '17 at 14:58
  • 1
    By the way, it's not kosher to put a Content-Type header in a GET request seeing as GET request can't have content! – ikegami Jul 19 '17 at 14:59
  • @Kumar: Your question is getting close to being closed. I suggest you carefully read the suggestions in these comments and then [edit your question](https://stackoverflow.com/posts/45190431/edit) to add the missing information. – Dave Cross Jul 20 '17 at 08:28
  • ok thanks tat code i changed some modifications and is working fine. – Kumar Jul 20 '17 at 10:07
  • 1
    @Kumar: If you have solved your own problem, please consider writing an answer (and even accepting it). That way, your question is useful for someone with a similar problem who is searching the site in the future. – Dave Cross Jul 20 '17 at 15:13

1 Answers1

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"; 
}
KSRKSR
  • 129
  • 1
  • 5