HttpClient Head request argument always null

1k Views Asked by At

I am building a WebAPI service that will be used for application updates, I have a HEAD and GET method written that both take a Version object which denotes the version that a client is upgrading to.

I have written a client for the application to use but have some issues passing the version object in the request, I have the following for the HEAD request

var httpClient = this.CreateHttpClient(this.ServiceBaseAddress, handler);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

JsonSerializerSettings settings = new JsonSerializerSettings
                                        {
                                            NullValueHandling = NullValueHandling.Ignore
                                        };
var serializedVersion = JsonConvert.SerializeObject(version, settings);

var requestUri = string.Format(
    "{0}{1}/?version={2}",
    this.ServiceBaseAddress,
    this.AddressSuffix,
    serializedVersion);

var request = new HttpRequestMessage(HttpMethod.Head, requestUri);
if (from.HasValue)
{
    request.Headers.Range = new RangeHeaderValue(from, to);
}
var response = await httpClient.SendAsync(request);

Now when I am debugging. the service method breakpoint is reached but the version parameter is always null, the service method signature is below

public async Task<HttpResponseMessage> Head(Version version)
{
...
}

Can anyone explain what I am doing wrong?

EDIT: Done some more analysis and I am getting a 415 Unsupported Media Type response from the server. I have added httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); but makes no difference, any ideas? (NOTE: I am getting this response either with the above or using version.ToString() as suggested)

1

There are 1 best solutions below

0
On BEST ANSWER

According to the parameter binding rules, Web API only gets values from the URI for "simple" types (string, int, DateTime, etc), Version not being among them. I do not believe that using the [FromUri] attribute would work either because Version is immutable (read-only properties) so I don't think Web API would know how to bind it.

So I think your only options would be to implement a custom type converter, which allows you to explicitly tell the framework how to convert a string to a System.Version, or if you're only using it in a couple of actions, keep it simple and use a string parameter type, and use Version.TryParse inside the action.