Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

I cannot execute a url request through Dancer

by logangha (Acolyte)
on Sep 22, 2022 at 23:56 UTC ( #11147067=perlquestion: print w/replies, xml ) Need Help??

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

Hi there
I am trying to make a request to a Dancer application which is initialized as I indicate:
# perl serv.pl
>> Dancer 1.3513 server 9802 listening on http://xxx.xx.xx.xx:xx
>> Dancer::Plugin::REST (0.11)
== Entering the development dance floor ...

But when I tried to execute below command
curl http://ipaddress:port/reference/C00-1412-0010

It shows this result:
curl http://ipaddress:port/reference/C00-1412-0010

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Error 500</title> <link rel="stylesheet" href="/css/style.css" /> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Error 500</h1> <div id="content"> <h2>Unable to process your query</h2>The page you requested is not ava +ilable </div> <div id="footer"> Powered by <a href="http://perldancer.org/">Dancer</a> 1.3513 </div> </body> </html>#

This is part of the code
package MyWebService; use strict; use warnings; use Dancer; use Dancer::Plugin::REST; use Config::File::Simple; use File::Slurp; use URI::Encode qw(uri_encode uri_decode); use IO::Socket; use IO::Select; use Storable; use Tie::IxHash; use REST::Client; use MIME::Base64; set server => 'xxx.xx.xx.xx'; get '/reference/:reference' => sub { writeLog("Arrived reference: " . params->{reference}); my (@arrRes, @arrNoRes) = processRequest(params->{reference}); if (@arrNoRes > 0) { push(@arrRes, @arrNoRes); } my $jsonResponse; if (@arrRes == 0) { $jsonResponse = to_json { reference => params->{reference} } +; } else { writeLog("Found results: " . Dumper(\@arrRes)); $jsonResponse = encode_json(\@arrRes); } return $jsonResponse; }; dance; . . .
Below are all the functions called for this code.
What is missing to allow this code works when it is invoked as a url with curl command?

Replies are listed 'Best First'.
Re: I cannot execute a url request through Dancer
by kcott (Archbishop) on Sep 23, 2022 at 02:47 UTC

    G'day logangha,

    In this code:

    my (@arrRes, @arrNoRes) = processRequest(params->{reference});

    Everything returned by processRequest() will go to @arrRes; while @arrNoRes will remain empty.

    From here, things will start to go wrong: for instance, the condition "@arrNoRes > 0" will always be false; and so on from there.

    I suggest you rewrite processRequest() to return two arrayrefs, instead of two arrays. Then make other changes from there as appropriate; e.g. "@$arrNoRes > 0".

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11147067]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-12-01 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?