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
I'm using the HttpWebRequestFilter property to do this
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,
>
Behaviour: Headers in the request will be replaced by the new Headers
dictionary, then the static hook will run, then the instance one.
- Nick
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
var httpReq = base.RequestContext.Get<IHttpRequest>();
httpReq.Headers
((HttpRequestWrapper)httpReq).Request
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...