1

I'm trying to force a request to my PHP API (based on a home-built framework with nginx) to result in a 502 bad gateway error so that I can test that it is handled gracefully in the application that calls the API. Is there an easy way to have the API cause a 502 error so I can test it?

Other errors (404, 500) are easy enough to produce and test, but I have no idea how to test a 502. If I understand correctly, it can be produced by sending an incomplete/invalid response but how do I trigger that?

Or would it be easier to temporarily configure nginx to produce it?

Update

Thanks for suggesting http_response_codein the comments, but it doesn't seem to be doing what I want. The one time the API actually resulted in a real 502 error, it broke the application that called it. I suspect setting the response code like this doesn't seem to cause a true 502 response since the application isn't acting the same way. I tried the following:

http_response_code(502);

http_response_code(502); die();

header('bad gateway', true, 502); die();

header('bad gateway', true, 502);

Chris
  • 4,277
  • 7
  • 40
  • 55
  • I think this is what you are looking for https://stackoverflow.com/questions/4797274/how-to-send-a-status-code-in-php-without-maintaining-an-array-of-status-names – RiggsFolly Jun 16 '17 at 10:28
  • 1
    [`http_response_code()`](http://php.net/manual/en/function.http-response-code.php)? – BenM Jun 16 '17 at 10:30

0 Answers0