<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> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:11.0pt'> I’m working on an API that spits out objects in JSON, and I have a function that handles errors. I can’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> </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> </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->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> </o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'> $r->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->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> </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->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->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 => $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 => $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> </o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'> $r->err_headers_out->{'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> </o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'># binmode(STDOUT, ":utf8");<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> </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> </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> </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> </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"'><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><html><head><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><title>400 Bad Request</title><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'></head><body><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><h1>Bad Request</h1><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><p>Your browser sent a request that this server could not understand.<br /><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'></p><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><hr><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'><address>Apache/2.4.7 (Ubuntu) Server at sherpaneo-test.sherpa.ac.uk Port 443</address><o:p></o:p></span></b></p><p class=MsoNormal><b><span lang=EN-GB style='font-size:11.0pt;font-family:"Courier New"'></body></html><o:p></o:p></span></b></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'>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> </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> </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"'>{"message":"unrecognised api key in 'api-key' at /usr/share/eprints3/lib/plugins/SherpaAPI.pm line 470.\n","status":400}<o:p></o:p></span></b></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'><o:p> </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 ‘200 OK’. 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> </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'>Cheers<o:p></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'>--<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>