I have a REST API with a function that returns a number (folio for me) and adds 1 to it and so on, if I run that function from Postam using the API URL it returns what I want, so far so good. Now how do I command to execute that function from C#?, I would not pass parameters to it, I just want it to execute the function and the response returned is a json_encode (message). I understand that it can be done with webclient downloadstring(url api) but it does not return the json, it only returns null. Any help would be welcome. Thanks in advance.
Here the code in PHP:
else if($_GET['url'] == "folio")
{
$respuesta = Clientes::ObtenerFolio();
$contenedor = array();
if($respuesta)
{
$contenedor["resultado"]="OK";
$contenedor["datos"]= $respuesta;
echo json_encode($contenedor);
}
else
{
echo json_encode(array(
"resultado" => 'NONE',
"mensaje" =>'No se pudo asignar folio al cliente'
));
}
This is from C# winforms:
try
{
string responsebody;
HttpClient cliente = new HttpClient();
HttpResponseMessage response = await cliente.GetAsync("http://www.mipagina.mx/WebService/folio");
response.EnsureSuccessStatusCode();
responsebody = await response.Content.ReadAsStringAsync();
dynamic datosdes = JsonConvert.DeserializeObject(responsebody);
string mjsserver = datosdes.resultado;
string msjservernot = datosdes.datos;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this: http://www.mipagina.mx/WebService/folio ", does it return any result? If it doesn't work then your rest endpoint is wrong
Sounds to me like what you have to do is something like this:
Well thanks to those who commented, everything in C# was correct, but in PHP I was receiving everything by
$_SERVER['REQUEST_METHOD'] == "POST"
and it is clear that from C# I doGetAsync
it because it was never going to return "something", so I did an else if after thePOST
so$_SERVER['REQUEST_METHOD'] == "GET"
and there inside I putand this returns the json and we swim, I deserialize it in C# and that's it. Thank you all.