boski
boski

Reputation: 1146

How to add own header to Azure Mobile Services call?

I've got Azure Mobile Service API and I would like to make call to it from Windows Phone application.

So I use something like that:

public static async Task<bool> InvokeGetUsers()
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("X-USER-TOKEN", App.userInfo.token);
            headers.Add("X-ZUMO-APPLICATION", "nxdQEvWOERLaHocwMz");

            Dictionary<string, string> arguments = new Dictionary<string, string>();
            arguments.Add("uuid", "123456");

            if (App.mobileServiceClient != null)
            {
                App.userFriends = await App.mobileServiceClient.InvokeApiAsync<List<GetUsers>>("get_users", System.Net.Http.HttpMethod.Post, arguments);
                return true;
            }
            return false;
        }

What I can't do is to pass header information to my call, how to do that?

Upvotes: 1

Views: 837

Answers (2)

Donal
Donal

Reputation: 32713

Ideally you want to add a header to all calls to your API through the MobileServiceClient. To do this you need to implement a Http Message Handler and pass that to the constructor of the MobileServiceClient, e.g.

App.mobileServiceClient = new MobileServiceClient(apiURI, new MyHandler());

Here is the implementation of the Handler:

public class MyHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("x-api-key", "1234567");
        var response = await base.SendAsync(request, cancellationToken);
        return response;
    }
}

Upvotes: 0

H&#229;kan Fahlstedt
H&#229;kan Fahlstedt

Reputation: 2095

There is an overloaded version of the InvokeApiAsync-method that you could use:

public Task<HttpResponseMessage> InvokeApiAsync(
    string apiName,
    HttpContent content,
    HttpMethod method,
    IDictionary<string, string> requestHeaders,
    IDictionary<string, string> parameters
)

More information here: https://msdn.microsoft.com/en-us/library/dn268343.aspx

Upvotes: 1

Related Questions