Forum

FCGI error reponse

Mike
17 December 2011, 17:50
Hello,

I'm writing an FCGI application behind Hiawatha. When there is an API error I would like to send an HTTP error code along with a response. The server sends the error to the client but the response JSON is replace with the Hiawatha error response pages, e.g. the 404 page, etc. I fiddled around with the "ErrorHandler" configuration but I couldn't figure out how to have it return the response from the application.

Cheers,
--
Mike

Hiawatha version: Hiawatha v7.6, cache, SSL, URL toolkit
Operating System: Linux 2.6.31 (embedded)
Hugo Leisink
17 December 2011, 18:00
To make Hiawatha send specific HTTP error codes, you have to send it via the Status header. Here are two examples to send the 500 Internal Server Error code:

PHP:
header("Status: 500");

C:
printf("Status: 500\r\n");
Mike
17 December 2011, 18:17
That part is fine, I can send the status header.

The problem is the body of the response, i.e. the JSON portion of the message.

I'm using libfcgi, so I want to do something like this:

FCGX_PutS("Content-type: application/json\r\n"
"Status: 400 Bad Request\r\n"
"\r\n"
"{\"message\":\"Body should be a JSON Hash\"}\n",
request->out);


Ideally the client would receive the error response (this part is working) and the response JSON. Currently the response JSON isn't reaching the client, instead I get the Hiawatha 400 page (HTML + some style information).

Thanks for the quick response,
--
Mike
This topic has been closed.