Discussion:
Use existing CGI response for HTTP errors
Daniel Becroft
2018-10-03 23:16:48 UTC
Permalink
Hi,

We are setting up Tomcat 8 to use a CGI program (.exe, proprietary) to
generate and return various JSON responses. This all works fine when the
response is a HTTP 200. But, when an HTTP error is returned (HTTP 4xx),
Tomcat is generating the HTML page instead.

We have the same setup working under IIS, and we had to configure the
following option there to stop IIS doing the same thing:

<httpErrors existingResponse="Passthrough">

Is there an existing option somewhere in Tomcat that will do the same thing
(ie keep the CGI response intact even if it's a HTTP error)? I can't seem
to find one.
---
Daniel Becroft
Mark Thomas
2018-10-04 13:59:07 UTC
Permalink
Post by Daniel Becroft
Hi,
We are setting up Tomcat 8 to use a CGI program (.exe, proprietary) to
generate and return various JSON responses. This all works fine when the
response is a HTTP 200. But, when an HTTP error is returned (HTTP 4xx),
Tomcat is generating the HTML page instead.
We have the same setup working under IIS, and we had to configure the
<httpErrors existingResponse="Passthrough">
Is there an existing option somewhere in Tomcat that will do the same thing
(ie keep the CGI response intact even if it's a HTTP error)? I can't seem
to find one.
No, but I think you can work-around it. If the response has been
committed (i.e. written to the network), Tomcat can't replace the error
page. Try adding a filter that calls ServletResponse.flushBuffer()
immediately after the CGI servlet has finished.

Something like (untested):

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request,response);
response.flushBuffer();
}
Post by Daniel Becroft
---
Daniel Becroft
---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@tomcat.apache.org
For additional commands, e-mail: users-***@tomcat.apache.org
Christopher Schultz
2018-10-04 17:12:33 UTC
Permalink
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Daniel,
Post by Daniel Becroft
Hi,
We are setting up Tomcat 8 to use a CGI program (.exe, proprietary)
to generate and return various JSON responses. This all works fine
when the response is a HTTP 200. But, when an HTTP error is
returned (HTTP 4xx), Tomcat is generating the HTML page instead.
We have the same setup working under IIS, and we had to configure
<httpErrors existingResponse="Passthrough">
Is there an existing option somewhere in Tomcat that will do the
same thing (ie keep the CGI response intact even if it's a HTTP
error)? I can't seem to find one.
See Mark's response for the "answer" to this; I had another question.

HTTP 4xx responses are usually telling the client that their request
was bad for some reason, and generally the response entity (the page
returned) is not relevant.

What kinds of responses are you returning for these errors? Or are you
trying to make sure that the response is still JSON even if an error
occurs?

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlu2SgEACgkQHPApP6U8
pFg9NA/+I2oFYemUtGs2MYJ+UKeB80SPtxdZyxdYdAVOqIa+hdq4cimxq648NGB0
eWalhK7PAn5d24XeUTCSoeKiBcn/CRvGW1E+2qv9tdXZRU7ZQneDWu4vDpm/VK1s
Jlv54CdhOr+0BaBY8Gsj/wGncMBpmG0XFNZxJXcGFb63Z9GMsaUxS18dfzC+Lbdc
k3Pg2/8s9+dMOwlGYjOZ+uK02Kk2XllCdZhn+rljoImqj1st23fwgdoI4FWho5Pa
H9KtWhteYNI6V7F59+5pbl8AyzkWvEyD/AsM2UwXMQBUTyuB0sNj+P5lXGZkDZn5
4eMfpUCbK2m9upquulOYGhtuYwfCqoc9zMIY7eu8mfHKfevmRL9+QBT8iz4QogHw
3F1nMm3wM3uZNInFoRTjfsHTcUdtSKRCAA4G5ocyGSezI4FWU/aWVMz27ZkoUV1e
205YN8+sjk25ihkNDsTZkqXiG3V7IC3Yqh3fiuZQGyL9Zv8GH0dKK9Nu4WTJgjZI
saHb+Wsghb3+B9dNMiI4MSrvTnmTUWDT70IX47ArpNH4C23aID9gpIjS72OniAuW
Il/a3McqyxR+T2sgaOLN+gY1FbW+XAGPhVQ8TZeywfBB//mdbPGqd24kg3ctEvPm
FWGYapZPIcF7i7RE0JaIZH0QH1wmrBqIPiSeKLxEtUA6RJV3Hg8=
=OCPo
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@tomcat.apache.org
For additional commands, e-mail: users-***@tomcat.apache.org
Daniel Becroft
2018-10-04 21:20:54 UTC
Permalink
On Fri, Oct 5, 2018 at 3:12 AM Christopher Schultz <
Post by Christopher Schultz
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Daniel,
Post by Daniel Becroft
Hi,
We are setting up Tomcat 8 to use a CGI program (.exe, proprietary)
to generate and return various JSON responses. This all works fine
when the response is a HTTP 200. But, when an HTTP error is
returned (HTTP 4xx), Tomcat is generating the HTML page instead.
We have the same setup working under IIS, and we had to configure
<httpErrors existingResponse="Passthrough">
Is there an existing option somewhere in Tomcat that will do the
same thing (ie keep the CGI response intact even if it's a HTTP
error)? I can't seem to find one.
See Mark's response for the "answer" to this; I had another question.
HTTP 4xx responses are usually telling the client that their request
was bad for some reason, and generally the response entity (the page
returned) is not relevant.
What kinds of responses are you returning for these errors? Or are you
trying to make sure that the response is still JSON even if an error
occurs?
- -chris
Thanks Chris.

We are using the response to return information about what was wrong with
the request, and how they might need to fix it.

For example:
{
"errorCode" : "400"
"errorMessages": [ "The customer cannot be terminated with an
outstanding invoice." ]
}

This is the format that our existing consumers are already receiving the
responses in, so we can't change that.

Cheers,
Daniel B.

Loading...