JsonServiceClient.PostAsync and custom HTTP headers

823 views
Skip to first unread message

Christopher House

unread,
Jul 30, 2012, 11:26:31 AM7/30/12
to servic...@googlegroups.com
Greetings -

I'm using JsonServiceClient on MonoTouch to call some RESTful services.  One of the requirements of the service that I'm calling is that I need to send a custom HTTP header for "security" purposes.  This was all working slendidly.  I was adding the custom header via the following code in the constructor of my class which calls the REST service:

            JsonServiceClient.HttpWebRequestFilter = filter => {
                filter.Headers.Add(string.Format("header name goes here: {0}", SECURITY_HEADER));
            };

However I've now refactored my code so that the service call is asynchronous.  Below is what my code looks like now that I've refactored:

        public void BeginSubmitEmployeeReferral(EmployeeReferral referral, Action<ServiceResponse> callback)
        {
            using (JsonServiceClient client = new JsonServiceClient())
            {
                client.PostAsync<ServiceResponse>(EMPLOYEE_REFERRAL_URL,
                                                  referral,
                                                  (response) => {
                    // Success
                    callback(response);
                },
                                                  (response, exception) => {
                    Console.WriteLine(exception.ToString()); // TODO:  Something better than this
                    callback(null);
                });
            }
        }

I'm using a web proxy (Charles) to capture all the HTTP traffic from my MonoTouch app.  I can see that since I've refactored to make my method async, the custom header is no longer added to the outgoing request and the response I get from the service is an HTTP 401 (expected behavior when the custom header isn't present). 

Is there a different way to set custom HTTP headers when using the async methods of JsonServiceClient?

Thanks!

Chris

Demis Bellot

unread,
Aug 13, 2012, 6:00:36 PM8/13/12
to servic...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages