0

I am trying to make a Http request, like the following:

var category = Uri.EscapeDataString("Power Tools");

var request = new HttpRequestMessage(HttpMethod.Get, $"/api/Items/GetAll?category={category}");

category now equals: Power%20Tools

The request gets translated to:

request = {Method: GET, RequestUri: 'http://localhost/api/Items/GetAll?category=Power Tools', ...

Why is HttpRequestMessage decoding my encoded string?

6
  • 1
    Where do you read the request? Commented Aug 4, 2021 at 12:38
  • @vernou I am debugging the method. I just hover over it. I have a Regular Expression validating input at the controller. It doesn't accept spaces. Commented Aug 4, 2021 at 12:41
  • 1
    I think you're going to need to include more detail. I just did a quick test and I don't see the same behavior. Commented Aug 4, 2021 at 12:49
  • @vernou I see http://localhost/api/Items/GetAll?category=Power%20Tools. However, the controller is seeing Power Tools and responds with an error message. My regex is ^[a-zA-Z0-9%]+$. This works if I add a space to the regex. Commented Aug 4, 2021 at 12:50
  • 1
    To be clearer: the value of Category is "Power Tools", but to represent it clearly in the URL we have to escape it (thus it becomes "Power%20Tools"). Because escaping just makes the URL work, it is decoded when ASP.NET passes it to the controller method. Commented Aug 4, 2021 at 12:58

1 Answer 1

3

I reproduce in Console app in .NET 5. I think, it's just the ToString that decode the url to be friendly on debug information. I don't find a information to this on the documentation, but .NET is now open source.

Generaly, the method ToString is used to generate debug information. See See the source code of HttpRequestMessage.ToString :

public override string ToString()
{
    StringBuilder sb = new StringBuilder();

    sb.Append("Method: ");
    sb.Append(method);

    sb.Append(", RequestUri: '");
    sb.Append(requestUri == null ? "<null>" : requestUri.ToString());
    ...
    return sb.ToString();
}

This just display requsetUri.ToString() and requestUri is type of Uri. From the official documentation of Uri.String:

The unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %.

// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.htm");

// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version.  OriginalString gives the orginal
// string that was passed to the constructor.

// The following outputs "http://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString());

// The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);
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.