0

I need some help to understand api and special char; i had trouble with xml content, now it is with strings

my API controller

[Route("Coordonnee/BySite/{siteCode}")]
[HttpGet]
public List<Coordonnee> GetDdpCoordonneeBySite(string siteCode)

I call it with HttpClient in C# programm like below

        HttpResponseMessage response = _getClient.GetAsync($"{uri}/{id}").Result;

        var respResult = string.Empty;
        if (response.IsSuccessStatusCode)
            respResult = response.Content.ReadAsStringAsync().Result;

        return respResult;

it works as long my SiteCode does not contains any special; if i put "TT+T" then gets Urlencoded like "TT&20&T" and the api is not called

Can someone explain me ? is it the controller ? or the call thanks;

Update : I test it also with swagger page : T+T in the param give this URI, it break the route and api is not called http://localhost:8085/Api/Ddp/Coordonnee/BySite/T%2BT

From my code, allways 404 not found, with or without UrlEncode...it is the same

GCamel
  • 612
  • 4
  • 8
  • In a URL `+` is a space, which is also encoded as `%20`. *Your* code specifically asked for `TT T`. As for the API not getting called, it's not due to the space. If the URL actually matched the route, the entire. string after `BySite/` would be passed as a `siteCode`. You need to URL encode your data, not just append it to the URL – Panagiotis Kanavos Jul 30 '21 at 09:21
  • Post an example that actually reproduces the problem and explicitly mention what happens. Do you get an exception in the client? What is the *actual* response? An exception in the server? What is the *actual* URL? Instead of `GetAsync($"{uri}/{id}")` store the URL in a variable so you can inspect it. You gain nothing by putting everything in a single line – Panagiotis Kanavos Jul 30 '21 at 09:24
  • BTW blocking async calls is a *very* bad idea. Returning empty on error is a *very very* bad idea, allowing the application to keep working even with bad data. Bugs like this have cost billions and are behind some of the most spectacular disasters. `await _client.GetStringAsync(url)` would have told you what's wrong at least – Panagiotis Kanavos Jul 30 '21 at 09:26
  • Refer this Post https://stackoverflow.com/a/13500078/4018180 – Akshay G Jul 30 '21 at 09:34
  • 1
    Use [WebUtility.UrlEncode](https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility.urlencode?view=net-5.0) to encode the string *before* appending it to the URL. Again, this wouldn't break routing. You probably have other errors, or the server returns an error that is ignored. – Panagiotis Kanavos Jul 30 '21 at 09:35
  • please, see my update. I searched lot about that and i understand the probleme. How to get an api that accept free text as parameter (like in a chat app) that can contains any char you do not manage ? is it on api or on the caller ? because even swagger does not manage that ? – GCamel Jul 30 '21 at 09:57

1 Answers1

0

According the snippets provided, I create a repository on GitHub. The following is the result: enter image description here

It seems GetAsync would encode '+'. Would you mind giving more information about the problem?

Chris Wong
  • 564
  • 4
  • 4
  • yes, that's true; encode is done but api function get never reached in debug and i got a 404 not founded. for me it is not NetCore; I have the same problem with swagger...will try another – GCamel Jul 30 '21 at 11:49
  • can u try to trace the request on both side ? – GCamel Jul 30 '21 at 12:00