Error passing parameter through Getasync method

Asked

Viewed 438 times

1

How I pass the object login as a parameter for the method GetAsync ? I’m trying to do it this way, but I didn’t get 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: inserir a descrição da imagem aqui

  • 1

    The message is quite explicit. The method Getasync expects a parameter of type Httpcompletionoption and an object like Login. The method Getasync does not have signatures allowing to pass objects: https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.getasync(v=vs.118). aspx

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 it as follows:

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

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

var result = await client.PostAsync(url, new StringContent(login, Encoding.UTF8, "application/json"));
  • Hello @Pedro Paulo I will change and put here the result.

  • All right, any questions I’m up for.

  • worked out, I just had to fit in that way: HttpResponseMessage response = await httpCliente.PostAsJsonAsync(url,login);. Thank you.

  • I’m glad it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.