Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I seem to be facing the same problem: I set header status to 400 in case of an error and return JSON array to describe the actual error.</p> <p>When I do:</p> <pre><code>print $main::cgi-&gt;header(@ret), $html; </code></pre> <p>With variables:</p> <pre><code>@ret: {'-type' =&gt; 'application/json','-charset' =&gt; 'utf-8','-status' =&gt; '400 Bad Request'} $html: '{"errors":{"short_name":["Missing!"]}}' </code></pre> <p>I will end up with this:</p> <pre><code>Status Code: 200 OK Content-Type: application/json; charset=utf-8 Response: {"errors":{"short_name":["Missing!"]}&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt; &lt;html&gt;&lt;head&gt; &lt;title&gt;400 Bad Request&lt;/title&gt; &lt;/head&gt;&lt;body&gt; &lt;h1&gt;Bad Request&lt;/h1&gt; &lt;p&gt;Your browser sent a request that this server could not understand.&lt;br /&gt; &lt;/p&gt; &lt;hr&gt; &lt;address&gt;Apache/2.2.3 (CentOS) Server at localhost Port 80&lt;/address&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>Using the method described by faquer will in deed suppress the error document, but still returns status 200 OK, like Jakub Narębski points out.</p> <hr> <p>BUT! I have found a workaround, where $r is a Apache2::RequestRec, using this: <a href="http://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Headers_Out" rel="nofollow">http://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Headers_Out</a> (otherwise you would use $r->send_http_header(), I guess)</p> <pre><code>print $main::cgi-&gt;header(@ret), $html; my $r = $main::cgi-&gt;r; $r-&gt;rflush; # force sending headers (with headers set by CGI) $r-&gt;status(200); # tell Apache that everything was ok, dont send error doc. </code></pre> <p>HTTP response:</p> <pre><code>Status Code: 400 Bad Request Content-Type: application/json; charset=utf-8 Response: {"errors":{"short_name":["Missing!"]}} </code></pre> <hr> <p>Apache config:</p> <pre><code>PerlModule ModPerl::PerlRun PerlModule CGI PerlModule Apache::DBI PerlRequire /var/www/html/startup.pl PerlSendHeader On </code></pre> <p>.htaccess:</p> <pre><code>&lt;Files *.cgi&gt; SetHandler perl-script PerlHandler ModPerl::PerlRun Options ExecCGI &lt;/Files&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload