http://www.perlmonks.org?node_id=1207050

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I want to send a post (ajax). I have a python code which works:

data=''; data = json.dumps(data).encode('utf-8') headers = {'__atts' : ajax_atts, '__ath' : ajax_ath, '__atcrv' : aja +x_atcrv } req = urllib.request.Request(url2)
but doing the same in perl it does not work: (all variables have the same values!)
my @data = (); my $json_data = encode_utf8(encode_json(\@data)); my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(POST => $url2); $request->header("__atts" => $ajax_atts); $request->header('__ath' => $ajax_ath); $request->header('__atcrv' => $ajax_atcrv); #$request->content($json_data); my $response = $ua->request($request);
Can you see the difference between this two programs ?

Thanks

Replies are listed 'Best First'.
Re: LWP: transfer from Python to Perl
by hippo (Bishop) on Jan 10, 2018 at 14:33 UTC
    it does not work

    Sure it does. You've just forgotten to use the relevant modules. If we add those three important lines to the start of your code it does indeed run fine to completion.

    use JSON; use Encode; use LWP::UserAgent; # Original content follows, unedited my @data = (); my $json_data = encode_utf8(encode_json(\@data)); my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(POST => $url2); $request->header("__atts" => $ajax_atts); $request->header('__ath' => $ajax_ath); $request->header('__atcrv' => $ajax_atcrv); #$request->content($json_data); my $response = $ua->request($request);

    "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be specific. SSCCE is best.

      Provided that the relevant modules happen to be available at runtime, there's also a comment sign preventing the JSON data to be included with the request. So, if "does not work" means "doesn't send JSON data" then just get rid of the # sign.
Re: LWP: transfer from Python to Perl
by Anonymous Monk on Jan 10, 2018 at 15:26 UTC

    Thanks hipo,haj for your comments !

    both code fragments are just fragments. The use commands I have at the beginning. Also the # sign was comming from trying different things out.

    "does not work" is indeed not specific! Sorry for that ! The issue is that the python version works fine. The server gives back the requested data. In the perl version the server response with an Error 500 Internal Server Error. That mens that something must be different in the two sended POSTs.

    When the line #$request->content($json_data) is commented out, than the server response an error 411 Length Required

    What would also help: Can I log what was exactly sent in both cases, to be able to compare all sent data ?

      In the perl version the server response with an Error 500 Internal Server Error.

      Without further details the best guess would be that you have not set the Content-Type header correctly (or at all). It also looks like you are attempting to set headers with leading underscores in the key. That's not going to work either.

      Can I log what was exactly sent

      Listen on the wire; see tcpdump for that. Otherwise use $request->dump.

        Hi hippo,

        Sorry for these basic questions. Thats one of my first web programs. I logged the packet:. I see some differences, but I can not really charge if this can be the rootcause: (first good one, second problem one)
        Accept-Encoding: identity Content-Type: application/x-www-form-urlencoded Content-Length: 2 User-Agent: Python-urllib/3.6 __Atts: 2018 __Ath: FnD= __Atcrv: 88 Connection: close TE: deflate,gzip;q=0.3 Connection: TE, close User-Agent: libwww-perl/6.26 __atcrv: 88 __ath: FnD= __atts: 2018 Content-Length: 2
        For the header key I saw also when I Dumper($request) the '_' were changed to '-'. (use Data::Dumper qw(Dumper); ) So I tried to write the header directly in the request structure.
        ${${$request}{'_headers'}}{'__atts'}=$ajax_atts; ${${$request}{'_headers'}}{'__ath'}=$ajax_ath; ${${$request}{'_headers'}}{'__atcrv'}=$ajax_atcrv2; print Dumper($request)
        Now the '_' are unchanged, but still error 500.
Re: LWP: transfer from Python to Perl
by jahero (Pilgrim) on Jan 12, 2018 at 08:44 UTC
    off topic:: Could I ask what is the motivation behind move from Python to Perl? Just curious, nothing more.
Re: LWP: transfer from Python to Perl
by Anonymous Monk on Jan 10, 2018 at 11:46 UTC
    upps. The I copied to less from my python code. Sorry !
    data=''; data = json.dumps(data).encode('utf-8') headers = {'__atts' : ajax_atts, '__ath' : ajax_ath, '__atcrv' : aja +x_atcrv } req = urllib.request.Request(url2) req.data = data; req.headers = headers; f = urllib.request.urlopen(req)