Call PUT method from an Xamarin.Forms App

Asked

Viewed 174 times

1

I need to do an update of mine App. I’m having some doubts to pass the proper parameters to the URL of the service and perform the PUT. The service is working, testing by Postman, I can perform the Update, so just missing by App. My difficulty lies in the passage of From the button event for the method and in the method object that I will assemble. Here is the event of the button click

private async void Aprovar(object sender, EventArgs e)
{
      string motivo = ent.Text;
      await dataService.UpdateProdutoAsync(??????, motivo);
}

The reason is correct. The other parameter is Marking. I have it somewhere, maybe in var _date, but the whole issue is in mine dataService, as below

public async Task UpdateProdutoAsync(int id, string value)
        {
            string url = $"http://meu_site/autorizador/api/itens/{id}/{value}";
            var uri = new Uri(string.Format(url, id));
            var data = JsonConvert.SerializeObject(??????);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            HttpResponseMessage response = null;
            response = await client.PutAsync(uri, content);

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Erro ao atualizar produto");
            }
        }

In the Jasonconvertobject is where the doubt lies. Assemble the object, if I’m passing primitive types only. I must pass the object Release, because in the service it represents an entity of the comic book, but I do not have it in this context, I have it before that screen, that it is passed by a GET.

EDIT1

I tried some solutions but still giving error: Here are the solutions tried. On the call screen of the Service, I have only items. Well, then I passed by the builder of this class the object Release. And at that point, I pass as a parameter to the method. It comes filled correctly, but is giving an untreated error, and the error says nothing. See the code as it was.

private async void btnItens_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new MainPageItens(_idorcamento, libera));
        }

The code, I have liberates which is the object I pass by the constructor of the class of Items

private async void Aprovar(object sender, EventArgs e)
        {
            string motivo = ent.Text;
            List<ItensLib> lib = new List<ItensLib>();
            lib = _data;

            var lista = liber
                .Where(l => l.IdOrcamento == IdOrcamento)
                .ToList();

            await dataService.UpdateProdutoAsync(IdOrcamento, motivo, lista);
        }

The code above is the click of the button, where I pick up the Release correct and step to the method.

public async Task UpdateProdutoAsync(int id, string value, List<Liberacao> liber)
        {
            string url = $"http://www.inetglobal.com.br/autorizador/api/itens/{id}/{value}";
            var uri = new Uri(string.Format(url, id));
            var data = JsonConvert.SerializeObject(liber);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            HttpResponseMessage response = null;
            response = await client.PutAsync(uri, content);

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Erro ao atualizar produto");
            }
        }

See in this report that there is information and it is correct inserir a descrição da imagem aqui

And this is Service, that is giving error. This is the Screenshot of the error: inserir a descrição da imagem aqui

EDIT2

I put a Try.. catch and picked up this message:

At Authorizer.Service.Dataservice+d__4.Movenext () [0x00152] in C: Mobile Authorizing Authorizing Authorizing Service Dataservice.Cs:62

What can make this mistake?

EDIT3

I’m pretty sure the problem is in the var date. She receives an object of the type Release and this object within my App, not exactly the same as the object within the service. This GET works, but the Get is already formatted the service and when receiving in the App, is already done the CAST for the guys who App will use. However, no PUT is different, the way is reverse, I pass the APP for the service and I think this is making this mistake.

1 answer

0

First I want to say that the problem is all mine. I was so immersed in the center of the problem that I didn’t look at the periphery of it. This made him unable to analyze each line and try to give a solution, but after a lot of prayer, there is no problem that God does not correct. My URL was wrong. I copied and pasted, added the parameters, but forgot to call the correct Service, which would be the Update and not the Items. Of course, that nobody but me could fix. I fixed it and I can update it. Now, it is to work on the colors, button events and take for the customer to see, pray and sell. Can curse me, just not my mother who is gone, rs.

string url = $"http://meu_site/autorizador/api/itens/{id}/{value}"; //errada

string url = $"http://meu_site/autorizador/api/atualiza/{id}/{value}"; //correta

Browser other questions tagged

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