0

estoy intentando consumir una api en c#, estoy trabajando en visual studio 2019, es de tipo POST, debo enviar un valor para que la api me devuelva algo, les muestro lo que tengo hasta ahora, no se como enviar tampoco el valor a la api, les agradecería mucho cualquier tipo de ayuda.


 DBApi dBApi = new DBApi();


 private void button1_Click(object sender, EventArgs e)
        {
              string json = "{\"saldo_total\":\"" + textBox1.Text + "\"}";
              dynamic respuesta = dBApi.Post("http://167.250.76.219/ws/onboarding/fincash/tarjeta_actual_fincash.php", json);
            textBox1.Text = respuesta;
           
                MessageBox.Show(respuesta.ToString());
           
            } ```

2 respuestas 2

2

Te recomiendo utilizar la librería de RestSharp, es fácil de utilizar. Te anexo un ejemplo.

 using RestSharp;
 public async Task<string?> SinglePost(int saldo_total)
        {
            var request = new RestRequest("http://167.250.76.219/ws/onboarding/fincash/tarjeta_actual_fincash.php");
            request.AddJsonBody(new { saldo_total });
            var response = await Client.ExecutePostAsync(request);
            if (response.IsSuccessful)
                return response.Content;
            else
                return response.ErrorMessage;
        }
1
0

Puedes usar HttpClient para consumir el api

var url = "https://api/post"; 
using(var httpClient = new HttpClient())
{
   var response = await client.PostAsJsonAsync(url, new Employee {Name = "Juan"});
   if (response.IsSuccessStatusCode)
   {
    //Peticion se ejecuto con exito recibes respuesta del api
   }
}


public class Employee
{
    public string Name{ get; set; }
}

como ves ejecutas una peticion post y usando el metodo PostAsJsonAsync

Te dejo un articulo en donde explico mas a detalle el uso de la clase HttpClient Como Consumir Apis en ASP.NET Core (IHttpClientFactory)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.