0

How to submit a valid JSON request using perl mechanize module

I tried

use WWW::Mechanize;
use JSON;   

my $mech=WWW::Mechanize->new(
    stack_depth     => 10,
    timeout         => 120,
    autocheck       => 0,
);

$mech->agent_alias( 'Windows Mozilla' );

my $json =  '{"jsonrpc":"2.0","id":1,"params":{"query":    {"limit":2000,"start":0,"orderBy":[{"columnName":"osName","direction":"Asc"}]},"refresh":true}}';

$url  ="http://path/to/url/";

$mech->post($url,$json);

and the result does not come as expected It always parse json error.

So an I doing it right by just posting $mech->post($url,$cjson);

or should I do / add something else?

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
Shanthi
  • 637
  • 2
  • 7
  • 17

1 Answers1

2

Normally one would use the JSON module so that you can make the data structure in Perl then serialize to a JSON formatted string.

$json_text = encode_json $perl_scalar

which would look something like this:

#!/usr/bin/env perl

use strict;
use warnings;

use JSON qw/encode_json/;

my $data = {
  "jsonrpc" => "2.0",
  "id" => 1,
  "params" => {
    "query" => {
      "limit" => 2000,
      "start" => 0,
      "orderBy" => [{ 
        "columnName" => "osName",
        "direction" => "Asc",
      }],
    },
    "refresh" => \0,
  },
};

print encode_json $data;

Note that \0 and \1 may be used as false and true respectively.

Then again, I haven't used WWW::Mechanize in a long time and I'm not going to dig into the docs, so here is an example using Mojo::UserAgent (more like LWP::UserAgent than mech), which has a built-in JSON handler:

#!/usr/bin/env perl

use strict;
use warnings;

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;

my $data = {
  "jsonrpc" => "2.0",
  "id" => 1,
  "params" => {
    "query" => {
      "limit" => 2000,
      "start" => 0,
      "orderBy" => [{ 
        "columnName" => "osName",
        "direction" => "Asc",
      }],
    },
    "refresh" => \0,
  },
};

my $url = "http://path/to/url/";
$ua->post( $url, json => $data );
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
  • Thanks a lot Joel for the json tip but it is not possible to install Mojo Useragent at all in my server and I'm stuck with perl WWW::Mechanize for the time being. I tried posting $mech->put($url,content => encode_json $json); $mech->post($url,content => encode_json $json); with the same results. Still not able to send correctly – Shanthi Apr 13 '13 at 13:59
  • 1
    @Shanthi try using LWP::UserAgent and do a post with that. You have that because it is a subclass of WWW::Mechanize. It's much more suited for your task than W:M. – simbabque Apr 13 '13 at 15:25
  • 2
    @simbabque, IIRC its the other way around, WWW::Mechanize is a subclass of LWP::UserAgent. – Joel Berger Apr 14 '13 at 00:35
  • @simbabque I tried that approach and the JSON problem is solved but the session/cookies is not being passed on to the mech object. $req = HTTP::Request->new( 'POST', $url ); $req->header( 'Content-Type' => 'application/json' ); $req->content( $json ); $mech->request($req); But now the original cookie jar data vanishes since this becomes a new request – Shanthi Apr 14 '13 at 02:31
  • @JoelBerger you are right, I meant a base class. Was in a hurry, thanks for correcting me. Shanthi: you need a dedicated cookie jar. Look at the docs of LWP. – simbabque Apr 14 '13 at 21:13