Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a JSON POST request with LWP?

If you try to login at https://orbit.theplanet.com/Login.aspx?url=/Default.aspx (use any username/password combination), you can see that the login credentials are sent as a non-traditional set of POST data: just a lonesome JSON string and no normal key=value pair.

Specifically, instead of:

username=foo&password=bar

or even something like:

json={"username":"foo","password":"bar"}

There's simply:

{"username":"foo","password":"bar"}

Is it possible to perform such a request with LWP or an alternative module? I am prepared to do so with IO::Socket but would prefer something more high-level if available.

like image 608
Richard Simões Avatar asked Nov 16 '10 21:11

Richard Simões


People also ask

Can we send JSON object in post request?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

How do I send a JSON post request in python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

Can you include JSON in a GET request?

To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).


3 Answers

You'll need to construct the HTTP request manually and pass that to LWP. Something like the following should do it:

my $uri = 'https://orbit.theplanet.com/Login.aspx?url=/Default.aspx';
my $json = '{"username":"foo","password":"bar"}';
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

Then you can execute the request with LWP:

my $lwp = LWP::UserAgent->new;
$lwp->request( $req );
like image 86
friedo Avatar answered Oct 19 '22 19:10

friedo


Just create a POST request with that as the body, and give it to LWP.

my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/json');
$req->content($json);

my $ua = LWP::UserAgent->new; # You might want some options here
my $res = $ua->request($req);
# $res is an HTTP::Response, see the usual LWP docs.
like image 28
hobbs Avatar answered Oct 19 '22 19:10

hobbs


The page is just using an "anonymized" (without name) input, which happens to be in JSON format.

You should be able to use $ua->post($url, ..., Content => $content), which in turn use the POST() function from HTTP::Request::Common.

use LWP::UserAgent;

my $url = 'https://orbit.theplanet.com/Login.aspx?url=/Default.aspx';
my $json = '{"username": "foo", "password": "bar"}';

my $ua = new LWP::UserAgent();
$response = $ua->post($url, Content => $json);

if ( $response->is_success() ) {
    print("SUCCESSFUL LOGIN!\n");
}
else {
    print("ERROR: " . $response->status_line());
}

Alternatively, you can also use an hash for the JSON input:

use JSON::XS qw(encode_json);

...

my %json;
$json{username} = "foo";
$json{password} = "bar";

...

$response = $ua->post($url, Content => encode_json(\%json));
like image 14
Paolo Rovelli Avatar answered Oct 19 '22 18:10

Paolo Rovelli