Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

perl dancer route template hashref pass complex json file to server issue

by RamiD (Acolyte)
on Jul 25, 2016 at 20:11 UTC ( #1168504=perlquestion: print w/replies, xml ) Need Help??

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

Hi , I have a problem with perl dancer post parameter to server with template hashref way, the param is json object, if the json object is simple , it work fine mean that perl subroutine template hashref send the json param obj as it should but if I have more complex json file eg. {"name":"jsonFileexample","problem":"thisIsThrProblem\"withComma"} if this is the json file with comma inside one of the json file param , perl hash ref delete the \ from the file , it will look like this {"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"} which will be not a valid json file , how can I send paran (json file) from perl dancer route template param to server as is , without perl changing my json file !!
  • Comment on perl dancer route template hashref pass complex json file to server issue

Replies are listed 'Best First'.
Re: perl dancer route template hashref pass complex json file to server issue
by stevieb (Canon) on Jul 25, 2016 at 21:01 UTC

    Your post, as is, is very hard to read (its one big blob of text).

    Please separate your paragraphs with <p></p> tags, and put your code within <code></code> tags to space it out a bit, and make it digestable (and the code/JSON copy/pastable).

Re: perl dancer route template hashref pass complex json file to server issue
by Anonymous Monk on Jul 25, 2016 at 22:38 UTC

    Hi,

    Which version of Dancer?

    Can you prove it? Can you post a short code example which demonstrates this bug?

      I don't see the claimed problem, it all works as it should
      >> Dancer 1.3202 server 2120 listenin >> Dancer::Plugin::EscapeHTML (0.22) http://localhost:3000/?jj={%22name%22:%22jsonFileexample%22,%22problem +%22:%22thisIsThrProblem\%22withComma%22};oj=jjjj
      {
        jj => "{\"name\":\"jsonFileexample\",\"problem\":\"thisIsThrProblem\\\"withComma\"}",
        oj => "jjjj",
      }

      use Dancer; use Data::Dump qw/ pp /; use Dancer::Plugin::EscapeHTML; any '/' => sub { "<pre>".escape_html( pp( scalar params() ) )."</pre>";;;; }; dance;
      Hi , I will explain more with code example in my perl dancer code I have route
      use Dancer ':syntax'; our $VERSION = '0.1'; get '/PopUp' => sub { my $jsonFile={jj =>"{"name":"jsonFileexample","problem":"thisIsThrProb +lem\"withComma"}"} template 'PopUp' ,{'sendfileToserver'=>$jsonFile}; };
      in the server side I have the route that receive the perl dancer paramemters
      <script> var customjsonList ="<%customCommands%>"; alert(customjsonList.children.length) var output=customjsonList .split(","); alert(output) </script>
      so here in javascript I had the error , that some thing is wrong with the json file when I look at the error I saw that json file is missing the \
      {"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"}" }
      I have perl dancer "Dancer 1.3202" thanks Rami D.

        Hi , I will explain more with code example in my perl dancer code I have route ... $jsonFile ...

        Sorry, that doesn't compile , its not perl

        $ perl nope Bareword found where operator expected at nope line 5, near ""{"name" (Missing operator before name?) String found where operator expected at nope line 5, near "name":"" Bareword found where operator expected at nope line 5, near "":"jsonFi +leexample" (Missing operator before jsonFileexample?) String found where operator expected at nope line 5, near "jsonFileexa +mple","" Bareword found where operator expected at nope line 5, near "","proble +m" (Missing operator before problem?) String found where operator expected at nope line 5, near "problem":"" Bareword found where operator expected at nope line 5, near "":"thisIs +ThrProblem" (Missing operator before thisIsThrProblem?) Backslash found where operator expected at nope line 5, near "thisIsTh +rProblem\" String found where operator expected at nope line 5, at end of line (Missing semicolon on previous line?) syntax error at nope line 5, near ""{"name" Can't find string terminator '"' anywhere before EOF at nope line 5.

        in the server side I have the route that receive the perl dancer paramemters

        That doesn't mention sendfileToserver

        so here in javascript I had the error , that some thing is wrong with the json file when I look at the error I saw that json file is missing the \

        So far you're not showing a problem with Dancer

        Most likely a problem in your template

        Use a JSON library to generate JSON values (we almost always use JSON::XS). Generating a JSON value by hand is error prone, especially when you don't know how escape characters work in quotes in the language you are writing in.

        #!/usr/bin/perl -l print qq<{"name":"jsonFileexample","problem":"thisIsThrProblem\"withCo +mma"}>;

        for example, produces:

        {"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"}

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2023-12-06 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (29 votes). Check out past polls.

    Notices?