Posted Mon, 27 Apr 2015 08:12:56 +0200 in reply to
Mathias LANG
Reply
Am 27.04.2015 um 05:14 schrieb Mathias LANG:
On Sun, 26 Apr 2015 19:49:22 GMT, Dato wrote:
I've been looking for this quite a time. I'd not open a thread for this dumb question but I couldn't find appropriate documentation. I found that @before annotation should be used, but I've got no idea how... So I have the following interface
...
@rootPathFromName
public interface ITroy
{
@path("/test")
string getTest();
}
...
with proper implementation. However, I've got no idea how to access headers from getTest method.
Thanks
Depends on what you want to do. If you want read-only access to the headers, you could use @headerParam
(example here: https://github.com/rejectedsoftware/vibe.d/blob/4bd0cbb1b4eb51051f7f7886e54c6f2c6b4541e1/examples/rest/source/app.d#L349 ).
And for more complex read requirements (e.g. processing multiple headers
at once), you can use
@before
.
You also always have the possibility to intercept the raw request:
void prepareRequest(HTTPServerRequest req, HTTPServerResponse res)
{
if (req.headers.get("My-Header") == "foo")
req.params["someParam"] = "bar";
}
interface MyRestInterface {
// _bar will be set by prepareRequest
void getTest(string _bar = null);
}
auto router = new URLRouter;
router.any("*", &prepareRequest);
router.registerRestInterface(...);
// ...
Or on the client side:
void prepareRequest(HTTPClientRequest req)
{
// ...
}
auto cli = new RestInterfaceClient!MyRestInterface;
cli.requestFilter = &prepareRequest;
Usually @before
- or better yet @headerParam
- is the cleaner
solution, but sometimes this can be more convenient if all requests are
to be handled uniformly.