3

I use documentation with WWWForm: https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html

C#:

void Start() {
    StartCoroutine(Upload());
}

IEnumerator Upload(){
    yield return Upload1();
    yield return Upload2();
}

IEnumerator Upload1() {
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
    //formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );

    UnityWebRequest www = UnityWebRequest.Post(url, formData);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "MULTIPART: " + www.downloadHandler.text );
    }
}

IEnumerator Upload2() {
    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");

    UnityWebRequest www = UnityWebRequest.Post(url, form);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "WWWForm: " + www.downloadHandler.text );
    }
}

PHP:

<?php 

    echo "POST: ";
    print_r( $_POST );
    var_dump( $_POST );

    echo "GET: ";
    print_r( $_GET );
    var_dump( $_GET );

?>

Response (MULTIPART and WWWForm):

POST: Array
(
)
array(0) {
}
GET: Array
(
)
array(0) {
}

$_POST is empty... (that's my problem)

Use Unity 2017.3.

I used UnityWebRequest.Send (Unity 5.6) before and it worked for me until it became obsolete.

Thanks.

Sergey
  • 33
  • 1
  • 4
  • I am also facing the issue and had been wondering whether the problem is with my code. But everything looks fine. Were you able to resolve it? – Neeraj Jaiswal Feb 14 '18 at 05:04

4 Answers4

7

This seems to work for me, I hope it helps.

UnityWebRequest www = UnityWebRequest.Post(url, formData);

www.chunkedTransfer = false;////ADD THIS LINE

yield return www.SendWebRequest();
Neeraj Jaiswal
  • 216
  • 1
  • 4
1

Right now I'm trying the same thing and since WWW class and WWWForm will be deprecated in the next version I tried to found a solution to this problem that does not use the deprecated class. In the end I understand that you don't actually need to use List if you just need to send a form with strings (example, right now I'm using it for a http post for code-flow auth in dropbox). Just use a Dictionary and you'll be fine (you will not even need to use www.chunkedTransfer = false;).

Dictionary<string, string> form = new Dictionary<string, string>();
form.Add("code", token);
form.Add("grant_type", "authorization_code");
form.Add("client_id", myid);
form.Add("client_secret", mysecret);
form.Add("redirect_uri", "http://" + localhost + ":" + port + '/');
LiefLayer
  • 977
  • 1
  • 12
  • 29
0

Use WWWForm instead. PHP is expecting application/x-www-form-urlencoded form data, not multipart.

WWWForm form = new WWWForm();
form.AddField("myField", "myData");

UnityWebRequest www = UnityWebRequest.Post(url, form);
Tom Mettam
  • 2,903
  • 1
  • 27
  • 38
  • I used both VVVForm and IMultipartFormSestion - the result is the same. In the Unity version 5.6 this worked, but with the Send method (which is now obsolete). Now the Send method does not work either. In any case, I need to send POST with the file (which also worked before). Thank you. – Sergey Feb 05 '18 at 20:55
0

I tried all of the above answers and nothing worked, but I noticedd that Unity adds a Request Header :

upgrade-insecure-requests  1

So I upgraded my url to https://

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

Sam Tyurenkov
  • 430
  • 4
  • 19