Code for Xamarin forms is giving me an error status for a BadRequest?

MJ_AhmedMJ_Ahmed BHMember ✭✭

Hi Guys,

I am trying to implement this Azure machine learning code for Xamarin forms but getting an error status as BadRequest... whats wrong in here :( ? Help

    private readonly string url = "URL OF MY WEBSERVICE FROM AZURE MACHINE LEARNING";
    private readonly string apiKey = "MY API KEY";

    HttpClient _client;
   public async void MachineLearning()
   {

        _client = new HttpClient();
        var scoreRequest = new
        {
            Inputs = new Dictionary<string, List<Dictionary<string, string>>>() {
                    {
                        "input1",
                        new List<Dictionary<string, string>>(){new Dictionary<string, string>(){
                                        {
                                            "Age_HR", "89"
                                        },

                                        {
                                            "Health_Type", "Fit"
                                        },
                            }
                        }
                    },
                },
            GlobalParameters = new Dictionary<string, string>()
            {
            }
        };

        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
        _client.BaseAddress = new Uri(url);
        var json = JsonConvert.SerializeObject(scoreRequest);
        HttpResponseMessage response = await _client.PostAsync("", new StringContent(json, Encoding.UTF8, "application/json"));

        if (response.IsSuccessStatusCode)
        {
            var resultado = await response.Content.ReadAsStringAsync();
            DisplayAlert("Alert",resultado.ToString(), "OK");
        }

        else
        {
            string Erro = (string.Format($"The request failed with status code: {response.StatusCode}"));
            await DisplayAlert("Erro", Erro, "ok");
        }
    }

}

Does any has a good sample to implementing azure machine learning webservice to Xamarin forms project??? Im stuck in my university project :(

Answers

  • kentuckerkentucker USMember ✭✭✭✭✭

    The first parameter in the PostAsync is the url to post to

            HttpResponseMessage response = await _client.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"));
    
Sign In or Register to comment.