mJay mJay - 5 months ago 79
C# Question

How to change the HTTP Request Content Type for FLURL Client?

I am using flurl to submit HTTP request and this is very useful. Now I need to change the "Content-Type" header for some of the requests to "application/json;odata=verbose"

public async Task<Job> AddJob()

var flurlClient = GetBaseUrlForGetOperations("Jobs").WithHeader("Content-Type", "application/json;odata=verbose");
return await flurlClient.PostJsonAsync(new
//Some parameters here which are not the problem since tested with Postman


private IFlurlClient GetBaseUrlForOperations(string resource)
var url = _azureApiUrl
.WithHeader("x-ms-version", "2.11")
.WithHeader("Accept", "application/json");
return url;

You can see how I tried to add the header above (
.WithHeader("Content-Type", "application/json;odata=verbose")

Unfortunately this gives me following error:

"InvalidOperationException: Misused header name. Make sure request
headers are used with HttpRequestMessage, response headers with
HttpResponseMessage, and content headers with HttpContent objects."

I also tried flurl's "ConfigureHttpClient" method but could not find how/where to set the content type header.

Answer Source

The comments and another post I found (will add reference when I find it again) have pointed me to the right direction. The solution for my problem looks like:

        var jobInJson = JsonConvert.SerializeObject(job);
        var json = new StringContent(jobInJson, Encoding.UTF8);
        json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; odata=verbose");

        var flurClient = GetBaseUrlForOperations("Jobs");

        return await flurClient.PostAsync(json).ReceiveJson<Job>();

Edit: Found the related SO question: Azure encoding job via REST Fails

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download