HttpClient / HttpRequestMessage accept header parameters cannot have spaces

Viewed 6511

I'm dealing with an API that requires me to set the header application/json;masked=false in order to unmask some information. When setting the header using

var request = new HttpRequestMessage()
request.Headers.Add("Accept", "application/json;masked=false");

it appears as though a space is being added between the ; and masked making the output header application/json; masked=false. Unfortunately this API I'm working with appears to be checking only against the literal application/json;masked=false without the space. I know the header works, because if I use it without the space in postman it works fine. If I use the one C# is generating in postman, it does not.

Is there any way to override this behavior?

Thanks

2 Answers

This problem still exists in .NET 5, but I solved it by using:

request.Headers.TryAddWithoutValidation("Accept", "application/json;masked=false");

No reflection needed when using this method.

Related