2

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--    
alag
  • 21
  • 3
  • This is nothing to do with PHP. You're not sending proper JSON, so if that's what the API is expecting you're not going to get very far. – miken32 Oct 31 '18 at 22:28
  • I have ran in circles with UWP ```HttpClient``` and the result was; ```HttpClient``` is bare and basically requires everything to be set manually. I would check the following: Make sure it's Put and not Post as you're using ```PutAsync```. Make sure your encoding is correct (which I'm sure it is). If you're using or modifying the headers make sure to clear before modifrying them with the old. This was my issue recently. ```miCliente .Headers.Clear();``` – Michael Puckett II Nov 01 '18 at 03:04
  • Is there any requirements for the httpclient [header](https://github.com/ZhuMingHao/httpClient/blob/master/HttpClientTest/HttpManager.cs#L51) within your php api? – Nico Zhu Nov 01 '18 at 09:50
  • @MichaelPuckettII Now I´m adding the headers manually but I have the same result :( – alag Nov 01 '18 at 14:11
  • Can you update the question with the entire payload shown in Postman? – Michael Puckett II Nov 01 '18 at 14:22
  • Updated with Postman, I´m not sure if this is what you´ve asked for. – alag Nov 01 '18 at 17:26

0 Answers0