I have a problem when I´m trying sending post and put requests to my webapi (the server side is coded in php and works with mysql).
If I try to send a get request there is no problem at all, the API responds properly, the problem comes when I try to send a body in request (that´s why I think post and put fails).
I am sure that my api works well, as long as I have done tests with postman and another clients and they all follow a correct way with these requests. The API responds 400 Bad Request and I don´t know what to do. I´ve done the same api coded in C# and my code in client-side works, there´s any incompatibility between Universal Windows Platform and PHP API´s?
My PUT method:
public async Task<int> updateFestivalDAL(Festival festival)
{
int resultado = 0;
Uri miConexion = (new clsMyConnection().getConnection());
HttpClient miCliente = new HttpClient();
String body = "";
HttpStringContent contenido;
HttpResponseMessage miRespuesta = new HttpResponseMessage();
try
{
body = JsonConvert.SerializeObject(festival);
contenido = new HttpStringContent(body, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
miRespuesta = await miCliente.PutAsync(new Uri(miConexion + "/" + festival.ID), contenido);
if (miRespuesta.IsSuccessStatusCode)
{
resultado = 1;
}
}
catch (SqlException e)
{
throw e;
}
return resultado;
}
That´s my body in request:
"{\"ID\":1,\"edicion\":\"Prueba año anterior\",\"nombre\":\"fgh\",\"fecha_inicio\":\"2017-10-01T00:00:00\",\"fecha_fin\":\"2017-10-03T00:00:00\",\"coordenadas\":\"asdf\",\"twitter\":\"\",\"facebook\":\"\",\"instagram\":\"\",\"web\":\"\",\"curiosidades\":\"\"}"
And that´s my API response (miRespuesta variable value):
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 2, Content:
Windows.Web.Http.HttpStreamContent, Headers:
{
Connection: close
Server: Apache/2.4.34 (Win32) OpenSSL/1.0.2o PHP/5.6.38
Date: Wed, 31 Oct 2018 21:47:36 GMT
X-Powered-By: PHP/5.6.38
}{
Content-Length: 0
Content-Type: text/html; charset=UTF-8
}}
Please help me if you know something.
UPDATE: When I see content of miCliente variable (the one with httpclient), I can see there´s a list element called DefaultRequestHeaders. Maybe there´s the problem? I have to edit these to make them compatible with PHP?
UPDATE 2: I´ve changed two dates elements (fecha_inicio, fecha_fin) in database and my Festival class, so they are now varchar (string at class), trying if the problem was parsing datetimes and try saving as date in database, but still not work.
Postman successfully request:
PUT /festival/1 HTTP/1.1
Host: notrelevantbutwellformed.com
Content-Type: application/json
cache-control: no-cache
Postman-Token: 100313d6-7087-4712-8b93-17873e1db14b
{
"ID": "1",
"edicion": "fgh",
"nombre": "fgh",
"fecha_inicio": "2018-11-01",
"fecha_fin": "2018-11-01",
"coordenadas": "asdf",
"twitter": "asfd",
"facebook": "ffsd",
"instagram": "asrss",
"web": "noo va a petar",
"curiosidades": "sdfsdfdsf",
"url_logo": "",
"url_cartel": ""
}------WebKitFormBoundary7MA4YWxkTrZu0gW--