in reply to Re: CGI: write exactly "\r\n" to end the headers, then turn off binmode
in thread CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
Thanks for the responses.
In a CGI script, you usually don't need to care about ending the headers. Just use the $cgi->header(...) method correctly.
In my case, I can't do that. I'm trying to read some simple json data in the body of a post request, but $cgi->{POSTDATA} gives me inconsistent results depending on my server. With an apache server, POSTDATA successfully contains the json. However, I tried my perl script on another server, and POSTDATA returns undef. To solve that issue, I'm reading from STDIN directly. I looked at the source code for CGI.pm, and I don't understand why CGI.pm fails to read the json while executing on my non-apache server, because I can get the json when I read from STDIN directly doing this:
(The cgi spec requires that a script not try to read more than Content-Length from STDIN.)
#!/usr/bin/env perl use strict; use warnings; use 5.020; use autodie; use Data::Dumper; use JSON; if (my $content_len = $ENV{CONTENT_LENGTH}) { read(STDIN, my $json, $content_len); #<===HERE ************ my $href = decode_json($json); my $a = $href->{a}; my $b = $href->{b}; print 'Content-type: text/html'; print "\r\n\r\n"; print "<div>a=$a</div>"; print "<div>b=$b</div>"; } else { my $error = "Could not read json: No Content-Length header in requ +est."; print 'Content-type: text/html'; print "\r\n\r\n"; print "<div>$error</div>"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
by Anonymous Monk on Mar 09, 2018 at 13:56 UTC | |
by 7stud (Deacon) on Mar 09, 2018 at 14:13 UTC | |
by Anonymous Monk on Mar 10, 2018 at 08:01 UTC | |
by 7stud (Deacon) on Mar 10, 2018 at 11:41 UTC |