Search code examples
c#http-headershttpclientspace.net-core

HttpClient in .netcore is automatically adding a space in header


I'm using HTTPClient in .netcore but I'm facing an issue when I add a custom accept header.

 var client = new HttpClient { Timeout = new TimeSpan(0, 5, 0)};
 client.DefaultRequestHeaders.Clear();
 client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");

But after this, what I see is that a space has been automaticaly added, resulting in "application/vnd.pagseguro.com.br.v3+json; charset=ISO-8859-1". That space is causing me a trouble because the server return me an error. Without that space it works.

There is any way to remove that space in HttpClient header? Conventional ways do not work, like trim, or just a replace once it's inside a DefaultRequestHeaders and I can't modify directly.


Solution

  • I was able to turnarround this issue by making a new microservice with .net Framework 4.5. Using HttpWebRequest, the header dont add an automaticaly space between the charset and accept.

    Thanks for the help =D