How to make a HTTP PUT request using LWP? - perl

I'm trying to change this request to a HTTP PUT request, any idea how ?
my $request = LWP::UserAgent->new;
my $response =
$request->get($url, "apikey", $apiKey, "requestDate", $requestDate);

You should use HTTP::Request:
use LWP::UserAgent;
use HTTP::Request;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new("PUT", $url);
my $res = $ua->request($req);

As of 6.04, LWP::UserAgent has a put helper, so you can now do:
$ua->put( $url )

PUT is HTTP::Request::Common. You can build the request first and pass it into user agent.
use HTTP::Request::Common;
use LWP;
$agent = LWP::UserAgent->new;
$request = HTTP::Request::Common::PUT($url, "apikey", $apiKey, "requestDate", $requestDate);
$response = $agent->request($request);

Related

How to make the post request in BOX API using LWP::UserAgent?

I have tried the following code
my $url = "https://api.box.com/2.0/users/";
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw{ POST };
use CGI;
my $ua = LWP::UserAgent->new();
my $request = POST( $url, [ 'name' => 'mkhun', 'is_platform_access_only' => 'true',"Authorization" => "Bearer <ACC TOK>" ] );
my $content = $ua->request($request)->as_string();
my $cgi = CGI->new();
print $cgi->header(), $content;
The above code always give the 400 error. And throwing the
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'is_platform_access_only=true&Authorization=Bearer+WcpZasitJWVDQ87Vs1OB9dQedRVyOrs6&name=mkhun'. Entity body should be a correctly nested resource attribute name\/value pair"}]},
I don't know what is the issue. The same thing with Linux curl is working.
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <TOKEN>" \
-d '{"name": "Ned Stark", "is_platform_access_only": true}' \
-X POST
The Box API documentation says:
Both request body data and response data are formatted as JSON.
Your code is sending form-encoded data instead.
Also, it looks like Authorization is supposed to be an HTTP header, not a form field.
Try this instead:
use strict;
use warnings;
use LWP::UserAgent;
use JSON::PP;
my $url = "https://api.box.com/2.0/users/";
my $payload = {
name => 'mkhun',
is_platform_access_only => \1,
};
my $ua = LWP::UserAgent->new;
my $response = $ua->post(
$url,
Authorization => 'Bearer <TOKEN>',
Content => encode_json($payload),
);

LWP POST request not working

The if statement is showing me that there is a response, but when I try to print the response I get nothing
use LWP::UserAgent;
use strict;
use warnings;
use HTTP::Request::Common;
# use this {"process": "mobileGps","phone": "9565551236"}
my $url = "the url goes here";
my $json = '{data :{"process" : "mobileGps", "phone" : "9565551236"}}';
my $req = HTTP::Request->new( POST => $url );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $ua = LWP::UserAgent->new;
my $res = $ua->request( $req );
if ( $res->is_success ) {
print "It worked";
print $res->decoded_content;
}
else {
print $res->code;
}
I do have the URL: I just took it out for the purpose of this example.
What am I missing?
Try debugging your script like this:
use strict;
use warnings;
use HTTP::Request::Common;
use LWP::ConsoleLogger::Easy qw( debug_ua );
use LWP::UserAgent;
# use this {"process": "mobileGps","phone": "9565551236"}
my $url = "the url goes here";
my $json = '{data :{"process" : "mobileGps", "phone" : "9565551236"}}';
my $req = HTTP::Request->new(POST => $url);
$req->header('Content-Type' =>'application/json');
$req->content($json);
my $ua = LWP::UserAgent->new;
debug_ua( $ua );
my $res = $ua->request($req);
if ($res->is_success) {
print "It worked";
print $res->decoded_content;
} else {
print $res->code;
}
That will (hopefully) give you a better idea of what's going on.
Can you not use the debugger, or add some print statements to see how your program is progressing?
If not then this is going to be another case of on-line turn-by-turn debugging, which benefits no one except the OP, and the ultimate diagnosis is that they should have learned the language first
The internet can be wise, but it will make many more artisans Pretender than craftsmen
Please don't ever expect to make a half-hearted attempt at a sketch, and then rope in the rest of the world to finish your job. It takes a huge amount of experience, aptitude, and understanding to get even a "What's your name" .. "Hello" program working, and things only get harder thereafter
If you don't like being careful and thorough, and would rather ask for people to do your stuff for you than discover a solution by experimentation, then you are a manager, not a programmer. I hope you will never try to advance a software career by getting great at delegating, because that doesn't work with software
Here. Use this as you will. The world is full of managers; it is good programmers that we need
use strict;
use warnings 'all';
use feature 'say';
use constant URL => 'http://example.com/';
use LWP;
my $ua = LWP::UserAgent->new;
my $json = '{}';
my $req = HTTP::Request->new( POST => URL );
$req->header( content_type => 'application/json' );
$req->content( $json );
my $res = $ua->request( $req );
say $res->as_string;
The code is fine. The problem must be with the server that is serving the request upon status code 200. You should check at server's end.

LWP::UserAgent and HTTP::Request for a POST request

In a certain script I tried to write this:
my $ua = LWP::UserAgent->new;
my $res = $ua->post($url, Content => $data);
and got "400 Bad Request".
After some reading I tried this:
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( 'POST', $url );
$req->content( $data );
my $res = $ua->request( $req );
and it worked, but I thought these two should do the same. What am I missing here?
Am I misunderstanding something in the documentation of HTTP::Request and LWP::UserAgent?
Is there a way to ask LWP::UserAgent to print what it is doing?
Here's one way to do it:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
{
no strict "refs";
no warnings "redefine";
my $orig_sub = \&LWP::UserAgent::send_request;
*{"LWP::UserAgent::send_request"} = sub {
my ($self, $request) = #_;
print $request->as_string . "\n";
my $response = $orig_sub->(#_);
print $response->as_string . "\n";
return $response;
};
}
my $a = LWP::UserAgent->new;
my $response = $a->get("http://google.com");
It will print out all the requests and responses that LWP::UserAgent does.

perl support IPv6 address format [::1] in HTTP::Request

Debian Wheezy (perl 5.14)
Work nice:
use LWP::Simple;
print get( 'http://ip6-localhost:80' );
Not working (1)
use LWP::Simple;
print get( 'http://[::1]:80' );
Not working (2) [Return: Bad hostname]
use LWP::Simple;
$ua = new LWP::UserAgent();
my $req = new HTTP::Request("GET", "http://[::1]/");
my $res = $ua->request($req);
Not working (3) [Return: Connection refused]
use Net::INET6Glue::INET_is_INET6;
use LWP::Simple;
$ua = new LWP::UserAgent();
my $req = new HTTP::Request("GET", "http://[::1]/");
my $res = $ua->request($req);
Why I need it? Because ldirectord need it. :(
Any suggestion?
Another post suggested using INET6Glue
use Net::INET6Glue::INET_is_INET6;
use LWP::Simple;
print get( 'http://[::1]:80' );
print get( 'http://ipv6.google.com' );

Mojo::UserAgent: get only the text

use WWW::Mechanize;
my $mech = WWW::Mechanize->new;
$mech->get( $url );
say $mech->text;
How could I get the same result with Mojo::UserAgent?
I tried this, but it doesn't return the same:
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
say $ua->get( $url )->res->dom->all_text;
Simply repeat what method text does: see as_text in HTML::Element.
You can try
$ua->get( $url )->res->dom->all_text(0);
for untrimmed output. Or you may need some kind of traversal over child nodes.