Search Unity

UnityWebRequest sending blank data to HTTP

Discussion in 'Scripting' started by xSmoking, Feb 28, 2018.

  1. xSmoking

    xSmoking

    Joined:
    Jun 18, 2015
    Posts:
    2
    This is a sample of what I'm doing to send data to my host, however, I think it's sending blank data. For example in the "serverKeycode", I'm sending "example", but the server is receiving just "".

    C#
    WWWForm form = new WWWForm();
    form.AddField("serverKeycode", "example");
    form.AddField("username", authUsername.text);
    form.AddField("password", authPassword.text);

    UnityWebRequest www = UnityWebRequest.Post("http://example.com/authenticate.php", form);
    yield return www.SendWebRequest();

    PHP
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');

    $server_key = "example";
    $server_auth = filter_input(INPUT_POST, 'serverKeycode');
    if ($server_auth == $server_key)
    {
    // do something​
    }
    else
    {
    $myJSON->returnType = 2
    $myJSON->message = "Key: " . $server_auth; // just for checking the key​
    }

    When returning a JSON from the PHP and printing the $message in Unity, it prints nothing.

    So does it mean that Unity is not sending the data?

    It used to work on my old webhost, also, if I send the POST Request through Postman, everything works fine. Do I need to allow something in the firewall?
     
    JamesSpavold likes this.
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    Try setting chunkedTransfer property on UnityWebRequest to false.
     
    xSmoking likes this.
  3. xSmoking

    xSmoking

    Joined:
    Jun 18, 2015
    Posts:
    2
    Thanks! That solved the problem.
     
  4. tonyram19

    tonyram19

    Joined:
    Jun 29, 2013
    Posts:
    8
    I'm having the same issue, even after setting chunkedTransfer to false. I've tried by passing a
    List<IMultipartFormSection>, a WWWForm, and regular strings to the Post method, but no luck. The body is always emtpy.

    Code (CSharp):
    1.      
    2.         WWWForm form = new WWWForm();
    3.         form.AddField("username_login", username);
    4.         form.AddField("password_login", password);
    5.         form.AddField("csrfmiddlewaretoken", theToken);
    6.  
    7.         www = UnityWebRequest.Post("https://synk.cloud/login/", form);
    8.  
    9.         www.SetRequestHeader("referer", "https://synk.cloud/login/");
    10.         www.SetRequestHeader("Content-Type", "form-data");
    11.         www.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0");
    12.         www.SetRequestHeader("X-CSRFToken", theToken);
    13.         www.SetRequestHeader("csrftoken", theToken);
    14.         www.SetRequestHeader("cookie", theTokenFull);
    15.  
    16.         www.chunkedTransfer = false;
    17.  
    18.         yield return www.SendWebRequest();
    19.  
    I would appreciate any help.
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    Try also setting useHttpContinue to false.
    Also, you probably shouldn't set Content-Type header, it will be set automatically.
     
  6. tonyram19

    tonyram19

    Joined:
    Jun 29, 2013
    Posts:
    8
    Thanks for the reply. Setting useHttpContinue to false didn't solve the issue, though. The POST request body is still not getting populated.
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    Can you report a bug for this? This does look like an issue.
     
  8. tonyram19

    tonyram19

    Joined:
    Jun 29, 2013
    Posts:
    8
    Sure, I will. Are there any known workarounds for this problem?
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    I haven't seen such issue before, please post the case number here once you have it, I'll try to make it go through faster.
     
  10. tonyram19

    tonyram19

    Joined:
    Jun 29, 2013
    Posts:
    8
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    Thanks, we'll look into it.
     
  12. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    77
    I am experiencing this issue too, but I'm using 2017.1.1f1
     
  13. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    I believe this bug has been fixed, so you need to upgrade.
     
  14. priyekantaghi

    priyekantaghi

    Joined:
    Nov 17, 2018
    Posts:
    1
    I am using Unity version 2018.1.0f2 and still facing this issue
     
  15. colourcrisis

    colourcrisis

    Joined:
    Apr 12, 2018
    Posts:
    3
    Still having this issue on 2019.2.0f1
     
  16. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,367
    Could you try the same request with most recent version of Unity? Report a bug, if it still reproduces there.
     
  17. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    26
    This might help someone:

    I tried all of answers from here and StackOverflow and nothing worked, but I noticed that Unity adds a Request Header :

    Code (CSharp):
    1. upgrade-insecure-requests  1
    So I upgraded my POST url to https://

    It still didn't work with
    Code (CSharp):
    1. MultipartFormData
    , but it started working with
    Code (CSharp):
    1. WWWForm form = new WWWForm();