Our Java apps use the Restlet framework, and we sometimes need to send custom headers in our HTTP responses. This is infrequent enough that we forget how to do so, and need to look it up, but frequent enough that we should really have this snippet close at hand.
So here’s how one adds a custom HTTP header to a Restlet Response:
Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
if (responseHeaders == null)
{
responseHeaders = new Form();
getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
}
responseHeaders.add("X-Some-Header", "the value");
姥姥 said:
Hi, just googled & pass by, I also need to customize HTTP response header for my restlet, thanks for the works, it save my time!
Art said:
Ditto. Thanks a lot. Saved my time as well.
Brian Bowman said:
In Restlet 2.0 at least, you can use HeaderConstants.ATTRIBUTE_HEADERS instead of “org.restlet.http.headers”.