JsonServiceClient - Send a custom HTTP Header

2,064 views
Skip to first unread message

Nick Malyon

unread,
Nov 23, 2011, 9:52:51 PM11/23/11
to ServiceStack .NET Open Source REST Web Services Framework
Hey guys,

Just trying ServiceStack.Client out for the first time. Is it
possible to send a custom HTTP header using the JsonServiceClient? I
am interfacting with the Postmark REST API and it requires me to
authenticate by sending a custom header...

public string GetPostmarkBounceDump(string id)
{
var restClient = new JsonServiceClient("https://
api.postmarkapp.com");
//TODO: Send custom header, X-Postmark-Server-Token: your-api-key-
here
var dump = restClient.Get<PostmarkBounceDump>(string.Format("/
bounces/{0}/dump", id));
return dump.Body;
}

Many thanks,
- Nick

Demis Bellot

unread,
Nov 23, 2011, 10:13:32 PM11/23/11
to servic...@googlegroups.com
It doesn't exist at the moment but feel free to add one, the base service implementation for all service clients is at:


Just needs a Dictionary<string,string> Headers { get; set; } property and you're good to go :)

Cheers,

Jon Canning

unread,
Nov 24, 2011, 2:26:36 AM11/24/11
to servic...@googlegroups.com

I'm using the HttpWebRequestFilter property to do this

Demis Bellot

unread,
Nov 24, 2011, 2:33:32 AM11/24/11
to servic...@googlegroups.com
Oh yeah that's an even better idea - no code changes required!

Note to self: Must remember cool hooks I've already added :)

Nick Malyon

unread,
Nov 24, 2011, 8:30:46 AM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Thanks Guys!

Solved with:
JsonServiceClient.HttpWebRequestFilter = x =>
x.Headers.Add(string.Format("X-Postmark-Server-Token: {0}",
postmarkAPIKey));

However, the filter is static which isn't really what I want in this
case, so I may still extend with either a Headers dictionary or an
InstanceHttpWebRequestFilter. Thoughts?

Cheers,
- Nick

On Nov 24, 7:33 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Oh yeah that's an even better idea - no code changes required!
>
> Note to self: Must remember cool hooks I've already added :)
>
>
>
>
>
>
>
>
>
> On Thu, Nov 24, 2011 at 2:26 AM, Jon Canning <jon.cann...@gmail.com> wrote:
> > I'm using the HttpWebRequestFilter property to do this

> > On Nov 24, 2011 3:13 AM, "Demis Bellot" <demis.bel...@gmail.com> wrote:
>
> >> It doesn't exist at the moment but feel free to add one, the base service
> >> implementation for all service clients is at:
>

> >>https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceS...
>
> >> Just needs a *Dictionary<string,string> Headers { get; set; }* property


> >> and you're good to go :)
>
> >> Cheers,
>

Dan B

unread,
Nov 24, 2011, 11:52:36 AM11/24/11
to servic...@googlegroups.com
Headers Dictionary sounds good to me (I need this functionality too!)

Cheers,

Dan

Nick Malyon

unread,
Nov 24, 2011, 5:52:59 PM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Ok, I'll do this tonight if I get time - will implement both on my
fork and submit a pull request for you to look over.

Behaviour: Headers in the request will be replaced by the new Headers
dictionary, then the static hook will run, then the instance one.

- Nick

Nick Malyon

unread,
Nov 24, 2011, 9:02:26 PM11/24/11
to ServiceStack .NET Open Source REST Web Services Framework
Implemented here: https://github.com/elasticsystems/ServiceStack

However, couldn't work out where I should add my tests or particularly
how to test this new logic within the existing framework - advice
anyone?

Cheers,
- Nick

Demis Bellot

unread,
Nov 25, 2011, 12:55:02 AM11/25/11
to servic...@googlegroups.com
Hey Nick this is great, can you send me a pull request when you're done.

It will require an integration test to test it properly, I'd probably create a Service that reads the headers and returns them in a Response DTO or something similar.
You can get the httprequest by inheriting from ServiceBase or RestServiceBase and do:

var httpReq = base.RequestContext.Get<IHttpRequest>();
httpReq.Headers

Should you need to you can also cast to get the strong-typed ASP.NET request object like:

((HttpRequestWrapper)httpReq).Request

Creating a new test class and service here would be a good place for it:

Nick Malyon

unread,
Nov 28, 2011, 9:08:17 PM11/28/11
to ServiceStack .NET Open Source REST Web Services Framework
Thanks Demis, will do that tomorrow!

On Nov 25, 5:55 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hey Nick this is great, can you send me a pull request when you're done.
>
> It will require an integration test to test it properly, I'd probably
> create a Service that reads the headers and returns them in a Response DTO
> or something similar.
> You can get the httprequest by inheriting from ServiceBase or
> RestServiceBase and do:
>
> var httpReq = base.RequestContext.Get<IHttpRequest>();
>
> > httpReq.Headers
>
> Should you need to you can also cast to get the strong-typed

> ASP.NETrequest object like:
>
> ((HttpRequestWrapper)httpReq).Request
>
> Creating a new test class and service here would be a good place for it:https://github.com/ServiceStack/ServiceStack/tree/master/tests/Servic...

Reply all
Reply to author
Forward
0 new messages