Search code examples
c#jsonwordpressxamarin.formswordpress-rest-api

Xamarin Forms - C# - HttpUtility.ParseQueryString - automatically formats a string?


I'm doing a basic API request using the ParseQueryString operator.

When I pass a particular value through the ParseQueryString, it automatically formats it.

For example, if I pass example@example.com through the ParseQueryString, the returned result would be :

example%40example.com

Which the server would return a null value.

However if the passed value is:

example@example.com

Then it will work.

I understand that query strings normally don't have "@" but is there a way to pass the "@" to the API server?

Here's my code:

var client = new HttpClient();

var queryString = HttpUtility.ParseQueryString(string.Empty);

queryString["email"] = LoggedIn.LoggedInEmail;

var apiuri = "https://example.com/wp-json/wc/v3/customers?" + queryString;

Console.WriteLine(apiuri);

The returned API is:

https://example.com/wp-json/wc/v3/customers?email=example%40example.com

When it should display like so:

https://example.com/wp-json/wc/v3/customers?email=example@example.com


Solution

  • Try to use HttpUtility.UrlDecode method after you generate the apiuri.

    var client = new HttpClient();
    
    var queryString = HttpUtility.ParseQueryString(string.Empty);
    
    queryString["email"] = LoggedIn.LoggedInEmail;
    
    var apiuri = "https://example.com/wp-json/wc/v3/customers?" + queryString;
    
    var url = HttpUtility.UrlDecode(apiuri,Encoding.UTF8);
    Console.WriteLine(url);