REST easy with kbmMW #19 – HTTP – Headers and cookies

kbmMW contains some nice features to get easy access to a headers and cookies. For example via attributes:

In the above example, accessing the server from a HTTP client like this:

Will result in the return of the value of the Accept header, which in my case resulted in:

Similarly requesting echocookie will return the value of the cookie named MyCookie, that the browser may have sent to the server. In my case, because there is no cookie defined with that name in the browser, the result will be empty.

This way you can easily get access to numerous header and/or cookie values.

But in the cookie scenario, we did not find any cookie because none was defined. So how do we return a cookie to the web client, that it can send to us next time?

Lets modify the EchoCookie method to return the cookie it received from the web client (as before), but then explicitly either create a new cookie in the client or update its existing value.

This example introduced two classes, TkbmMWHTTPTransportHelper, and TkbmMWHTTPCookie.

With each call to a method in a service, two transport streams are made available, a request transport stream (self.RequestTransportStream) which contains what we received from the caller augmented with various data the application server sees fit to augment with, and a response transport stream (self.ResponseTransportStream) which will contain the data we want to return to the caller.

Part of the response transport stream will be the result of this method (a string) along with the response MimeType we defined using the attributes. However there are many other header type values that can be returned, like cookies and such (when its a response for a HTTP type client).

Each transport stream has a helper attached, which makes certain specialized features easier available. When the transport stream format of the transport, has been set to REST (which it is in our case), we can rest assured that the helper must be a TkbmMWHTTPTransportHelper or a descendant of that. So casting it nonchalantly will work fine in this case, but if the EchoCookie has dual use, meaning it can be called from a non REST type client (which it could if we added the attribute [kbmMW_Method] in the interface section for that particular method, then we should actively test if the helper is indeed a TkbmMWHTTPTransportHelper before using it as such.

Thats not the case in our sample here so lets continue.

Now we have access to a helper object (hlp). In that helper object you will find loads of nice things, like access to the Cookies property, where you can get access to all cookies provided by the web client (browser) if you are looking via the RequestTransportStream’s helper, or access to all the cookies you want to return to the web client if you are looking via the ResponseTransportStream.

Thus we check if MyCookie exists. If it does not, then we create a new one which is automatically added. Afterwards we update its value and when the cookie expires.

So now when we first time call the echocookie REST call, we will again receive an empty result (still no cookies defined), but 2nd time we make the call, the ISO8601 timestamp of the first call is returned as the cookie value, and the cookie is then updated again and its expiration extended.

As you may have guessed by now, the hlp instance is also the entrance to accessing and manipulating header values.

Let us make a method that returns any header value for any header name we specify in the call:

And its implementation:

It again cast to get a specialized helper, but now on the RequestTransportStream, and then return the value of a specific header item.

Will in my case return

Some HTTP headers may contain qualifiers or options as part of the headers value. For example the Accept-Language header that often is sent with every browser request, and that tells the server which spoken language(s) the client will accept the response in (or more accurately, prefer the response in). Example:

In this example, the browser absolutely prefer all returned data to be localized into German.

However if this value was provided by the browser, the server should send data localized into German if at all possible, else it should be localized into English.

Hence the Accept-Language is in fact a value that contains underlying fields (de and en) which in turn may have options attached to them (q).

kbmMW provides a way to access these (already parsed) values too:

As you can see, each level has the AsString and AsEncodedString propertty accessible, which makes it possible to set or get the textual representation of that particular value in the mime header value hierarchy.

If you like kbmMW, share the word. Reshare the blog posts and let others know about the product!

Essentially help us to help you 🙂

927 total views, 3 views today

Author: kimbomadsen

Leave a Reply

Your email address will not be published. Required fields are marked *

two + fifteen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.