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)