0

I need to consume a 3rd party api of an internal system, which expects the following headers:

Accept application/json;charset=iso-8859-1
Content-Type application/json;charset=iso-8859-1

In order to do this I've cobbled together the following piece of code:

public async Task<bool> CreateOrDeleteFolder(CreateFolderRequestBody body)
{
   var request = new HttpRequestMessage(HttpMethod.Post, $"{BaseAddress}/testFolder.php");
   request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")
   {
      CharSet = "iso-8859-1"
   });
   
   var isoEncoding = Encoding.GetEncoding("iso-8859-1");
   var payload = new StringContent(JsonConvert.SerializeObject(body), isoEncoding, "application/json");

   request.Content = payload;
   HttpResponseMessage responseMessage = null;
   
   responseMessage = await _client.SendAsync(request);
  
   responseMessage.EnsureSuccessStatusCode();
   var responseBody = await responseMessage.Content.ReadAsStringAsync();
   //do something with the body
   return true;
}

This produces the following request:

POST http://somewhere.to/testFolder.php HTTP/1.1
Accept: application/json; charset=iso-8859-1
Content-Type: application/json; charset=iso-8859-1
Host: somwhere.to
Content-Length: 57
Expect: 100-continue
Connection: Keep-Alive

Apparently the additional whitespace in the headers between the semicolon and the charset produces a response stating invalid header values.

How can I manipulate those header values to not contain those white spaces?

5
  • Ahm, I know that this might sound weird, but can you try adding User-agent to your request and see if that helps, despite the ; space Commented Oct 9, 2020 at 12:20
  • In Postman I have PostmanRuntime/7.26.5 as the user-agent and adding removing the whitespace reproduces the issue consistently. Commented Oct 9, 2020 at 12:24
  • So apparently this is a bug in.net core and if you use full .net framework 4.5 it may be avoided. But not later versions of .NETFX. o_O. github.com/dotnet/runtime/issues/30171 It devolved into the usual potato-potato argument with .net devs saying 'we are following RFC' :/ Commented Oct 9, 2020 at 12:39
  • Also, apparently partially addressed in .net 5.0, so wait another month and you should have it :) github.com/dotnet/corefx/pull/41640 Commented Oct 9, 2020 at 12:40
  • I'm on full 4.6.2, and I need it done yesterday - as always. So no luck there ;) Commented Oct 9, 2020 at 12:42

1 Answer 1

1

I've botched a workaround together by inherting from MediaTypeHeaderValue:

public class TrimmedMediaTypeHeaderValue : MediaTypeHeaderValue
{
   public TrimmedMediaTypeHeaderValue(string mediaType) :base(mediaType)
   {

   }

   public override string ToString() 
        => string.IsNullOrEmpty(CharSet)
           ? MediaType
           : $"{MediaType};charset={CharSet}";
}

Since the docs did not specify any method that would build the header string together I just assumed it would be ToString() and indeed - it is. So this now produces a header value, without a whitespace between the ; and the charset.

Be aware, that this class has only one constructor instead of the 2 of the base class. So if you want to use this, you might need to add another one in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.