2

I'm developing a RESTful API and I wrote a mod_perl2 handler that takes care of the request.

My handler deals with error codes by setting $r->status($http_code) and return $http_code;

Everything is fine, except a little problem: when my http_code is different than 200 (for instance 404), apache appends a default HTML error document to my own generated response.

For instance:

GET /foo

Gives:

$VAR1 = bless( {
                 'status' => 404,
                 'data' => {},
                 'message' => 'Resource not found for foo'
               }, 'My::Response' );
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /foo was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache/2.0.54 (Fedora) Server at localhost Port 80</address>
</body></html>

How do I get rid of this apache generated HTML?

UPDATE: My fault. My mod_perl2 handler was returning a HTTP_* code instead of Apache2::Const::OK.

jeje
  • 3,191
  • 3
  • 26
  • 41
  • How would you tackle this problem with Apache 1.3 and mod_perl 1.xx when Apache::Constants don't support HTTP status codes like 201? – Gaurav Dadhania Jun 01 '11 at 03:51

3 Answers3

2

I was looking for this too. And the trick was quite simple:

$r->status(HTTP_NOT_FOUND);
$r->custom_response(404, "");
return OK;

where $r is Apache2::Response object.

Kakash1hatake
  • 128
  • 12
  • The only thing that sucks here with mod_perl, is not being able to set a custom `content-type` when using `custom_response`. It's always set as `text/html`. – Francisco Zarabozo Nov 08 '18 at 06:01
  • You can easily set the content type you want with `$r->content_type( 'application/json' )` for example. See [mod_perl2 documentation](https://perl.apache.org/docs/2.0/api/ModPerl/Registry.html#top). – Jacques Dec 16 '20 at 07:48
1

See Apache2::Response. I do not have time to experiment right now, but that should work.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • That's the idea, but I've got this error from apache: ErrorDocument takes two arguments, Change responses for HTTP errors Any mean to deactivate the apache error documents? I don't think this is a problem since the return code is kept. Some browser choose to display their own error documents instead of apache ones for instance. – jeje Nov 05 '09 at 15:30
0

Are you asking how to send no message body in your response?

If you want something other than what apache is going to do for you, you need to handle the request yourself. What does the rest of your handler look like? Posting code keeps us from guessing what you are doing.

The return value from your handler lets apache know if you handled the request yourself or if it needs to do something more on your behalf. I'm guessing that you're doing the latter.

brian d foy
  • 129,424
  • 31
  • 207
  • 592