RejectedSoftware Forums

Sign up

Access headers in REST api?

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

Re: Access headers in REST api?

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 ).

If you want to write to them, that's a bit more problematic. ATM there is no perfect solution. One way to do it is to use @after https://github.com/rejectedsoftware/vibe.d/blob/4bd0cbb1b4eb51051f7f7886e54c6f2c6b4541e1/examples/rest/source/app.d#L289 , however, bear in mind it has to be trivial since it will leak on the client side.

Re: Access headers in REST api?

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.

Re: Access headers in REST api?

Thank you very much guys. One more thing, I'm not sure I understand what does the string parameter (in this case "user") does in

@before!authenticate("user")

and what's the 'result' parameter of addBrackets?

string addBrackets(string result /* THIS ONE */, HTTPServerRequest, HTTPServerResponse)
{
	return "{" ~ result ~ "}";
}

Is this the generated response string?

Thanks again :)

Re: Access headers in REST api?

Also, I forgot to ask, does @before means that authenticate will be invoked before any REST API call? I mean, if I invoke getSecret for example, vibed will invoke authenticate and getSecret after that? In this case(if my definition is true), what's the purpose of returning an User object from authenticate? Who will see that object..?

On Tue, 28 Apr 2015 17:07:10 GMT, Dato wrote:

Thank you very much guys. One more thing, I'm not sure I understand what does the string parameter (in this case "user") does in

@before!authenticate("user")

and what's the 'result' parameter of addBrackets?

string addBrackets(string result /* THIS ONE */, HTTPServerRequest, HTTPServerResponse)
{
	return "{" ~ result ~ "}";
}

Is this the generated response string?

Thanks again :)

Re: Access headers in REST api?

On Tue, 28 Apr 2015 17:07:10 GMT, Dato wrote:

Thank you very much guys. One more thing, I'm not sure I understand what does the string parameter (in this case "user") does in

@before!authenticate("user")

It means the function that's annotated with @before has a parameter named 'user', and the return of authenticate will be passed as parameter. Thus, the return type of authenticate must be the same (or implicitly convertible to, I guess) the type of the parameter 'user'.

and what's the 'result' parameter of addBrackets?

string addBrackets(string result /* THIS ONE */, HTTPServerRequest, HTTPServerResponse)
{
	return "{" ~ result ~ "}";
}

Is this the generated response string?

Thanks again :)

Yes. It's basically the return of the function, so it doesn't have to be a string.

Re: Access headers in REST api?

On Tue, 28 Apr 2015 17:14:57 GMT, Dato wrote:

Also, I forgot to ask, does @before means that authenticate will be invoked before any REST API call? I mean, if I invoke getSecret for example, vibed will invoke authenticate and getSecret after that? In this case(if my definition is true), what's the purpose of returning an User object from authenticate? Who will see that object..?

Before only applies to the function it annotate. As mentionned in my other reply, the object will be passed as a parameter.
Please don't forget that authenticate needs to be trivial or you'll run into issues.

Re: Access headers in REST api?

Aaah, how could I be so dumb :)). I see now, clean and nice. Thank you very much again guys, you saved my day :)