<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=Title content=""><meta name=Keywords content=""><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Courier New";
        panose-1:2 7 3 9 2 2 5 2 4 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:Calibri;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:Calibri;
        color:windowtext;}
span.msoIns
        {mso-style-type:export-only;
        mso-style-name:"";
        text-decoration:underline;
        color:teal;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:Calibri;}
@page WordSection1
        {size:595.0pt 842.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body bgcolor=white lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>Hi<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>                I&#8217;m working on an API that spits out objects in JSON, and I have a function that handles errors.  I can&#8217;t seem to get it to behave, and I wonder if anyone can offer me any advice.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>sub api_error<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>{<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        my ($repo, $code, $message) = @_;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        my $r = $repo-&gt;request;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        $r-&gt;status($code);<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        EPrints::Apache::AnApache::send_status_line( $r, $code );<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        $r-&gt;content_type('application/json');<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        my $json = JSON-&gt;new;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        my $content = $json-&gt;encode(<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        {<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>                status =&gt; $code,<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>                message =&gt; $message<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        }<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        );<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        $r-&gt;err_headers_out-&gt;{'Content-Length'} = length $content;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>#       binmode(STDOUT, &quot;:utf8&quot;);<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>#       print $content;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>        return $code;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>}<o:p></o:p></span></b></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>Using CURL to look at the headers and body, I get this:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>HTTP/1.1 400 Bad Request<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Date: Tue, 26 Jun 2018 15:48:51 GMT<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Server: Apache/2.4.7 (Ubuntu)<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Content-Length: 120<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Connection: close<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Content-Type: text/html; charset=iso-8859-1<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;!DOCTYPE HTML PUBLIC &quot;-//IETF//DTD HTML 2.0//EN&quot;&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;html&gt;&lt;head&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;title&gt;400 Bad Request&lt;/title&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;/head&gt;&lt;body&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;h1&gt;Bad Request&lt;/h1&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;p&gt;Your browser sent a request that this server could not understand.&lt;br /&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;/p&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;hr&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;address&gt;Apache/2.4.7 (Ubuntu) Server at sherpaneo-test.sherpa.ac.uk Port 443&lt;/address&gt;<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>&lt;/body&gt;&lt;/html&gt;<o:p></o:p></span></b></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>If I uncomment the two commented lines, I get this:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>HTTP/1.1 200 OK<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Date: Tue, 26 Jun 2018 15:48:36 GMT<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Server: Apache/2.4.7 (Ubuntu)<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Content-Length: 120<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>Content-Type: application/json<o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'>{&quot;message&quot;:&quot;unrecognised api key in 'api-key' at /usr/share/eprints3/lib/plugins/SherpaAPI.pm line 470.\n&quot;,&quot;status&quot;:400}<o:p></o:p></span></b></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>Essentially, if I output anything to STDOUT, something (maybe MOD Perl) switches the status to &#8216;200 OK&#8217;.  How can I get the status code from the first version, and the body from the second?  Any ideas?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>Cheers<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>--<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'>Adam Field<o:p></o:p></span></p></div></body></html>