Search Unity

Question Unity post data to laravel issues

Discussion in 'Multiplayer' started by Vlahotic, Jun 3, 2021.

  1. Vlahotic

    Vlahotic

    Joined:
    Nov 20, 2020
    Posts:
    5
    Hi

    I'm trinyg to post data from my unity app to php/laravel.
    I have tested the api in postman and it returns 200/OK but when I send the request in unity I keep getting 500 error.

    The request on the server shows up as empty, without any body data.


    Code (CSharp):
    1. WWWForm form = new WWWForm();
    2.  
    3.         form.AddField("token", tokenvalue);
    4.         form.AddField("email", email);
    5.         form.AddField("lat",latitude );
    6.         form.AddField("long", longitude );
    7.         form.AddField("startTime", "2011-11-11 11:11:11");
    8.         form.AddField("currentTime", currentTime);
    9.         form.AddField("fullName", fullName);
    10.  
    11. UnityWebRequest www = UnityWebRequest.Post(url , form);
    12.         Debug.Log(www);
    13.  
    14.         www.SetRequestHeader("Content-Type", "application/json");
    15.         www.SetRequestHeader("Authorization", "Bearer XlOorHn8dMthtbdpGE6yaf7wx0q7rfzZ7L4x6tduP5oYO4gSrr7lhpmj5RBW");
    16.         yield return www.SendWebRequest();
    I am catching the response and have code that does other things based if it's 'OK'.
    All of the values in the fields are string variables as expected.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    If you expect your WWWForm to be sent as JSON, you are wrong. It sends HTML form. You need to manually serialize JSON.
     
  3. Vlahotic

    Vlahotic

    Joined:
    Nov 20, 2020
    Posts:
    5
    Even without the
    www.SetRequestHeader("Content-Type", "application/json");
    the request ends up being empty.

    That was just my last ditch effort to get the request to go through
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    How are you reading the request? Are you reading the POST fields?
    You may also try disabling HTTP-Continue on the request. Some servers have issues with that.
     
  5. Vlahotic

    Vlahotic

    Joined:
    Nov 20, 2020
    Posts:
    5
    There were some server-side issues that got resolved with API changes (app/server that I am communicating with is not my project).
    After that & with the removal of header content type the request works

    Issue resolved