4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I don't think so. It sounds strange though. The server should accept it. Spaces are allowed according to the standard. – Patrick Hofman Oct 20 '16 at 11:08
  • I think too. But sadly the UOL servers don't accept with space. I Tested also on postman. But thanks for the comment Patrick – Robson Paproski Oct 20 '16 at 11:24
  • What is UOL...? – Patrick Hofman Oct 20 '16 at 11:26
  • It's a Brasilian payment solution. I'm already entered in contact with them. But i'm still waiting an answer. The full name of the payment provider is PagSeguro. For international payment i'm using Paypal. But for Brasil, paypal dont support direct Credit Card Payment. – Robson Paproski Oct 20 '16 at 11:34
  • what if add header per request using HttpRequestMessage.Headers.TryAddWithoutValidation ? – Set Oct 20 '16 at 12:29
  • Even using with HttpRequestMessage. Have any other class to make a post request in .netcore? – Robson Paproski Oct 20 '16 at 13:11
  • This behavior is hardcoded into HttpClient. See here: https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.EasyRequest.cs#L744 – Homr Zodyssey Oct 20 '16 at 16:57
  • Thanks all the comments. i was able to turnarroud this issue making a new microservice with .net framework 4.5 and HTTPWebRequest – Robson Paproski Oct 20 '16 at 18:38

2 Answers2

1

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

0

This is a bug in corefx.

Still open after 3 years!

https://github.com/dotnet/corefx/issues/39260

Sourcerer
  • 1,891
  • 1
  • 19
  • 32