2

I am writing a new endpoint in my HTTP service that is built on the Play framework, and am returning a custom status code for a particular error (442 in this case). When I test the endpoint with cURL, I receive the error code as expected:

...
< HTTP/1.1 442 Client Error (442)
....

And the same with Postman REST Client:

Postman status with custom response code

What I would like to do is customize the "Client Error" text, such that the response would actually read something like:

442 Forced Password Reset

Is this possible, or is it in the spec somewhere that any custom status codes of the 4xx class are to be interpreted by all clients simply as "Client Error"?

(I have been looking through the relevant Play documentation on Statuses but don't see any option to customize the text—only the status code integer itself.)

1 Answer 1

5

Looking through the source code it didn't take me long to find the following:

  1. Play just stores the status code in the RepsonseHeader, not the string
  2. Play uses Netty, and turns the status code into a HttpResponseStatus using valueOf, which is basically just a case statement over the status codes, with defaults based on the range if it isn't standard.
  3. Although you could define a new Netty HttpResponseStatus with the reason phrase you want, there isn't any way to add it to the valueOf method (it's static).

So, there isn't any really good solution without rewriting some parts of Play!.

You might be able to use some sort of post-filter to modify the response and change the reason phrase, but I don't know how that would work, or even if it is possible to write those kinds of filters in Play!.

Finally, the reason phrase isn't really that important, as clients generally don't (and shouldn't) actually parse it.

Sign up to request clarification or add additional context in comments.

1 Comment

That's the answer I suspected (but wasn't hoping for). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.