Asked  7 Months ago    Answers:  5   Viewed   303 times

I'm trying to set the Content-Type header of an HttpClient object as required by an API I am calling.

I tried setting the Content-Type like below:

using (var httpClient = new HttpClient())
    httpClient.BaseAddress = new Uri("");
    httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
    httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
    // ...

It allows me to add the Accept header but when I try to add Content-Type it throws the following exception:

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

How can I set the Content-Type header in a HttpClient request?



The content type is a header of the content, not of the request, which is why this is failing. AddWithoutValidation as suggested by Robert Levy may work, but you can also set the content type when creating the request content itself (note that the code snippet adds application/json in two places-for Accept and Content-Type headers):

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("");
      .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
request.Content = new StringContent("{"name":"John Doe","age":33}",
                                    "application/json");//CONTENT-TYPE header

      .ContinueWith(responseTask =>
          Console.WriteLine("Response: {0}", responseTask.Result);
Tuesday, June 1, 2021
answered 7 Months ago

By default your route will look something like this:

    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }

When you visit the url http://{domain}/api/foo/{username} the controller is mapped as foo and the optional id parameter is mapped to {username}. As you don't have a Get action method with a parameter called id a 404 is returned.

To fix this you can either call the API method by changing the URL to be explicit about the parameter name:


Or you could change your parameter name in your action method:

public Foo Get(string id)
    var foo = _service.Get<Foo>(username);
    return foo;

Or you could change your route to accept a username:

    name: "DefaultApi",
    routeTemplate: "api/{controller}/{username}",
    defaults: new { username = RouteParameter.Optional }
Tuesday, August 17, 2021
answered 4 Months ago


  • HttpClient can only be injected inside Typed clients
  • for other usages, you need IHttpClientFactory
  • In both scenarios, the lifetime of HttpClientMessageHandler is managed by the framework, so you are not worried about (incorrectly) disposing the HttpClients.


In order to directly inject HttpClient, you need to register a specific Typed service that will receive the client:

services.AddHttpClient<GithubClient>(c => c.BaseAddress = new System.Uri(""));

Now we can inject that inside the typed GithubClient

public class GithubClient
    public GithubClient(HttpClient client)
        // client.BaseAddress is ""

You can't inject the HttpClient inside AnotherClient, because it is not typed to AnotherClient

public class AnotherClient
    public AnotherClient(HttpClient client)
        // InvalidOperationException, can't resolve HttpClient 

You can, however:
1. Inject the IHttpClientFactory and call CreateClient(). This client will have BaseAddress set to null.
2. Or configure AnotherClient as a different typed client with, for example, a different BaseAdress.


Based on your comment, you are registering a Named client. It is still resolved from the IHttpClientFactory.CreateClient() method, but you need to pass the 'name' of the client


services.AddHttpClient("githubClient", c => c.BaseAddress = new System.Uri(""));


// note that we inject IHttpClientFactory
public HomeController(IHttpClientFactory factory)
    this.defaultClient = factory.CreateClient(); // BaseAddress: null
    this.namedClient = factory.CreateClient("githubClient"); // BaseAddress: ""
Tuesday, September 14, 2021
answered 3 Months ago

Use the Response.ContentType property at the top of your .cshtml file then include the XML in the content of the view:

   Response.ContentType = "application/xml";
<?xml version="1.0" encoding="UTF-8"?>
Thursday, October 7, 2021
Adam Rackis
answered 2 Months ago

I've never had any success on the client-side either, but it is supposed to. Check out the client part of the meteor HTTP package:

Mostly it uses the browser XHR object on the client-side which can lead to a host of problems, like incompatibilities and stuff. You can even see an issue referenced on one of the code comments (around line 136)

And when you check out the server implementation you can see it uses the request library (from connect), which, in my book, is very reliable and you can generate uniform results across all users (and not dance around browser differences).

My choice and recommendation for you is obviously going to be server-side calls. Not just because it works and it's reliable, it's also 'safer' on your part as you don't have to expose more inner workings of your system to the client/end-user. Who knows? maybe you have sensitive data on your API run on your Python-based server.

Wednesday, October 27, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :