Search code examples
c#mauirestsharpmaui-blazor

Problem with api request using RestSharp in .Net Maui


I'm newbie to .Net Maui and RestSharp, I'm mostly Unity developer. I have this request

using RestSharp;

namespace FindPlacesApp.Code.ApiRequests
{
    public class SearchPlacesRequest
    {
        public async Task<string> GetPlaces(double latitude, double longitude, int radius)
        {
            try
            {
                var options = new RestClientOptions("https://api.foursquare.com/v3/places/search");

                var client = new RestClient(options);

                var request = new RestRequest();

                request.AddHeader("Accept", "application/json");

                request.AddHeader("Authorization", Constants.apiKey);

                request.AddQueryParameter("ll", $"{latitude},{longitude}");

                request.AddQueryParameter("radius", radius.ToString());

                var response = await client.GetAsync(request);

                if (!response.IsSuccessStatusCode) await AlertDisplayer.DisplayError($"Unsuccess: {response.StatusCode}");

                return response.Content;

            }
            catch (Exception ex)
            {
                await AlertDisplayer.DisplayError($"Exception: {ex}");
            }
            return string.Empty;
        }
    }
}

It perfecly works when on Windows build, but on Android build i get exception "Bad Request" on the line var response = await client.GetAsync(request); The problem not in latitude, longitude, radius or Internet status, I checked that. Is theare problems with RestSharp on Android using .Net Maui or I am doing something wrong?


Solution

  • I have fix my problem. I moved from RestSharp to httpclient but that dont help me too much. then after some tests I find out that latitude and longitude formating on Android was wrong and not the same as on my Pc. I fix that by this way:

    string formattedLatitude = latitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
    string formattedLongitude = longitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
    

    Here is my end and working varinat

    namespace FindPlacesApp.Code.ApiRequests
    {
        public class SearchPlacesRequest
        {
            public async Task<string> GetPlaces(double latitude, double longitude, int radius)
            {
                try
                {
                    string formattedLatitude = latitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
                    string formattedLongitude = longitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
    
                    var client = new HttpClient();
                    var request = new HttpRequestMessage()
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri($"https://api.foursquare.com/v3/places/search?ll={formattedLatitude}%2C{formattedLongitude}&radius={radius}"),
                        Headers =
                        {
                            { "accept", "application/json" },
                        }
                    };
    
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", Constants.apiKey);
    
                    var response = await client.SendAsync(request); 
    
                    response.EnsureSuccessStatusCode();
    
                    var contentString = await response.Content.ReadAsStringAsync();
    
                    return contentString;
    
                }
                catch (Exception ex)
                {
                    await AlertDisplayer.DisplayError($"Exception: {ex}");
                }
                return string.Empty;
            }
        }
    }