Error passing parameter through GetAsync method

0

How do I pass the login object as a parameter to the GetAsync method? I'm trying to do it this way, but I did not understand the error message:

   private async Task<JsonResult> obterLogin(Login login)
            {
                try
                {
                    HttpClient httpCliente = new HttpClient();
                    httpCliente.BaseAddress = new Uri("http://localhost:55838/");
                    httpCliente.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await httpCliente.GetAsync("MedClinApi/Login/Obter/ { login }", login);
                    var json = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<JsonResult>(json, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });      
                }
                catch
                {
                    throw;
                }
            }

Error Print:

    
asked by anonymous 27.07.2018 / 15:43

1 answer

1
The problem is that you are passing your parameter incorrectly, if you are using C # version 6 or higher you can pass parameter using string interpolation:

HttpResponseMessage response = await httpCliente.GetAsync($"MedClinApi/Login/Obter/{ login }");

If you are using a lower version of C #, use the following:

HttpResponseMessage response = await httpCliente.GetAsync(string.Format("MedClinApi/Login/Obter/{0}", login));

This way you will put your parameter in the url, however it makes more sense to send an object in the body of the HTTP request using the POST method. If this is your service I suggest you change to POST. Here is an example:

var result = await client.PostAsync(url, new StringContent(login, Encoding.UTF8, "application/json"));
    
27.07.2018 / 15:59