7

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

4
  • 4
    Take a look at the source for WebRequest at referencesource.microsoft.com/#System/net/System/Net/…. As for a fix, it doesn't look good, as all headers go through some 'tidy up', probably due to it being in some RFC somewhere. If you can work out where the API you are using is wrong in the RFC, it might be easier to tell them they have a non-conforming implementation and they should fix it.
    – Neil
    Commented Jul 19, 2017 at 15:24
  • 2
    FYI just tried it and this is the same in Framework 4.5. Not sure it can be easily fixed at your end (you know things are bad when you're reading pages on w3c.org). Commented Jul 19, 2017 at 15:27
  • @Neil Thanks for the source link! I honestly forgot about that. At this point I'm thinking you're probably right, and we need to contact them and tell them their API is non-conforming.
    – Thomas F.
    Commented Jul 19, 2017 at 15:38
  • 2
    @DaveBecker Yeah pretty much lol. Thanks for testing it in Framework though!
    – Thomas F.
    Commented Jul 19, 2017 at 15:39

2 Answers 2

8

Alright, so through some digging, we ended up finding this github issue for the problem: https://github.com/dotnet/corefx/issues/18449 where they have a workaround which uses reflection.

I adopted their workaround to what I'm doing like so:

        request.Headers.Add("Accept", contentType);
        foreach (var v in request.Headers.Accept)
        {
            if (v.MediaType.Contains("application/json"))
            {
                var field = v.GetType().GetTypeInfo().BaseType.GetField("_mediaType", BindingFlags.NonPublic | BindingFlags.Instance);
                field.SetValue(v, "application/json;masked=false");
                v.Parameters.Clear();
            }
        }
2
  • Thank you for posting all of this. I have a ticket open with, what I assume is, the same company you're having this space issue with with unmask sensitive data. Hopefully they conform to the standard soon. Thank you very much for posting your workaround. Glad I'm not the only one facing the same issues.
    – Matt H
    Commented Jul 20, 2017 at 19:11
  • @MattH No problem, and if it's a company that deals with pay, we probably are :) We had actually e-mailed them and they notified us of that open issue, then said they can't support it :/ But, the issue is in .netcore "Future" milestone, so hopefully they either conform or MS lets us force using exactly what we put in.
    – Thomas F.
    Commented Jul 20, 2017 at 19:43
5

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.