RejectedSoftware Forums

Sign up

REST API and access Http headers and query string

Hello,

What is the recommanded way to access and set http headers in a rest api impl?
Using @before / @after or add an 'any' tot the router?
Same question for accessing uri query string part.

Thanks.
Arjan

Re: REST API and access Http headers and query string

On Sun, 19 Oct 2014 13:06:08 GMT, Arjan wrote:

Hello,

What is the recommanded way to access and set http headers in a rest api impl?
Using @before / @after or add an 'any' tot the router?
Same question for accessing uri query string part.

Thanks.
Arjan

It always depends on the use case, but both ways can make sense (using the router when all routes need to do the same thing and using an attribute when only some of the routes are affected). You can also make an enum out of @before/@after annotations, so that you have more compact and readable attributes (e.g. @authenticated instead of @before!performAuth("_authUserInfo"): enum authenticated = before!performAuth("_authUserInfo").

There is also currently an open pull request (which I still need to review) that will add @fromQuery and @fromHeader attributes.

Re: REST API and access Http headers and query string

On Mon, 20 Oct 2014 07:31:53 GMT, Sönke Ludwig wrote:

On Sun, 19 Oct 2014 13:06:08 GMT, Arjan wrote:

Hello,

What is the recommanded way to access and set http headers in a rest api impl?
Using @before / @after or add an 'any' tot the router?
Same question for accessing uri query string part.

Thanks.
Arjan

It always depends on the use case, but both ways can make sense (using the router when all routes need to do the same thing and using an attribute when only some of the routes are affected). You can also make an enum out of @before/@after annotations, so that you have more compact and readable attributes (e.g. @authenticated instead of @before!performAuth("_authUserInfo"): enum authenticated = before!performAuth("_authUserInfo").

There is also currently an open pull request (which I still need to review) that will add @fromQuery and @fromHeader attributes.

Thanks. Will take a look at that pull request to see whether or not it suit my needs.

Arjan

Re: REST API and access Http headers and query string

On Mon, 20 Oct 2014 10:50:25 GMT, Arjan wrote:

On Mon, 20 Oct 2014 07:31:53 GMT, Sönke Ludwig wrote:

There is also currently an open pull request (which I still need to review) that will add @fromQuery and @fromHeader attributes.

Thanks. Will take a look at that pull request to see whether or not it suit my needs.

Arjan

Note: Only @fromRequest is in the PR, but @fromQuery should follow pretty soon, should the design suits you Sönke ;)

If you just need to get a value from the header / query in the server, @before would be fine.
You however have to know that the function must be visible to the interface, thus it will leak in the client side. So avoid anything that's not self-contained.
In addition, it works server-side, but not client-side (meaning that you cannot send a parameter through header with the REST generator, you have to do it by hand).

Re: REST API and access Http headers and query string

Am 20.10.2014 14:52, schrieb Geod24:

Note: Only @fromRequest is in the PR, but @fromQuery should follow pretty soon, should the design suits you Sönke ;)

I've just taken a very quick look and I think it should be fine. I
currently don't have the time for a proper review, but I'll try to do
that as soon as the 0.7.21 release is done.

If you just need to get a value from the header / query in the server, @before would be fine.
You however have to know that the function must be visible to the interface, thus it will leak in the client side. So avoid anything that's not self-contained.
In addition, it works server-side, but not client-side (meaning that you cannot send a parameter through header with the REST generator, you have to do it by hand).

Why doesn't it work on the client? I definitely think it should.

Re: REST API and access Http headers and query string

On Thu, 23 Oct 2014 13:46:06 +0200, Sönke Ludwig wrote:

Am 20.10.2014 14:52, schrieb Geod24:

If you just need to get a value from the header / query in the server, @before would be fine.
You however have to know that the function must be visible to the interface, thus it will leak in the client side. So avoid anything that's not self-contained.
In addition, it works server-side, but not client-side (meaning that you cannot send a parameter through header with the REST generator, you have to do it by hand).

Why doesn't it work on the client? I definitely think it should.

Example:
https://github.com/rejectedsoftware/vibe.d/blob/master/examples/rest/source/app.d#L398

As you guess, the parameter is send, but rewritten by UDA function (That's explicitly documented in the code: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/web/rest.d#L533 ). So whatever you send is passed using the regular parameter passing technique (Query/Body for GET/POST, respectively), then it will be ignored, as @before will override it.

That issue can be summed up by: @before can be a great tool, but everything that's put on the interface must have a client/server (sender/receiver) meaning, and @before is only meaningful to the receiver.