Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Print Json text from query string

by perlCrazy (Monk)
on Sep 10, 2012 at 10:40 UTC ( #992717=perlquestion: print w/replies, xml ) Need Help??
perlCrazy has asked for the wisdom of the Perl Monks concerning the following question:

I am creating URI and sending JSON string in URL. while recieveing not able to print. Cab somebody please help?

cleint request code snippet:
my $json = { 'username' => 'foo', 'password' => 'bar', }; my $encoded = to_json($json); my $escaped = uri_escape($encoded); #string. my $query = '&inputparms='.$escaped; my $url = $rest_url.$query; print "$url\n"; my $response = $ua->post($rest_url); my $content = undef; if ($response->is_success) { $content = $response->content; }

server side code snippet:
my $input = $PARMS{inputparms}; print qq{Content-type: application/json; charset=utf8\n\n}; my $in = from_json( $input, {utf8 => 1} ); my $jsonDecode = encode_json($in); print "$jsonDecode\n";

Replies are listed 'Best First'.
Re: Print Json text from query string
by Corion (Pope) on Sep 10, 2012 at 10:47 UTC

    Please post self-contained relevant code. The code you show is not all code that is relevant. You don't show how %PARMS gets populated for example.

    Also, for a HTTP POST request, it is weird/uncommon to send the parameters in the URL string instead of as POST parameters. This may or may not contribute to a problem on the receiving end.

      Thanks for the response.
      Yes, intentionally have not posted the entire code. Please assume that query string has JSON string and server code should read and print inputparms. if i get that part, issue should be resolved.
      Thanks again for your time.

        So, if you already have verified that your server program does see the correct data, why are you still posting both halves of the application? Or did you not verify that your server program gets the correct data?

        If you already know that your server program receives data, why did you not hardcode the received data and checked it? Eliminating the client from the problem helps reduce the number of points of failure.

        I recommend you use CGI to parse your query data in the server program. If you are using hand-rolled code to parse the query data, please eliminate that in favor of CGI or another query parser module. You should be aware that there are multiple ways to pass query data in POST requests, and I already told you that your way of passing it is different from other ways. CGI can handle both ways, but it's unclear whether your unshown code can handle them

Re: Print Json text from query string
by rpnoble419 (Pilgrim) on Sep 10, 2012 at 17:12 UTC
    Why do you want to create a URL? it looks like you get JSON in from some ajax call, why not push JSON back? Otherwise you could just send the following:
    print $url.'/'.$json->{'username'}.'/'.$json->{'password'}

    Also why are you passing passwords in the open?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992717]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-06-18 06:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.