Version: 2018.1
Downloading an AssetBundle from an HTTP server (GET)
Uploading raw data to an HTTP server (PUT)

Sending a form to an HTTP server (POST)

There are two primary functions for sending data to a server formatted as a HTML form. If you are migrating over from the WWW system, see Using WWWForm, below.

Using IMultipartFormSection

To provide greater control over how you specify your form data, the UnityWebRequest system contains a user-implementable IMultipartFormSection interface. For standard applications, Unity also provides default implementations for data and file sections: MultipartFormDataSection and MultipartFormFileSection.

An overload of UnityWebRequest.POST accepts, as a second parameter, a List argument, whose members must all be IMultipartFormSections. The function signature is:

WebRequest.Post(string url, List<IMultipartFormSection> formSections);

Details

  • This function creates a UnityWebRequest and sets the target URL to the first string parameter. It also sets the Content-Type header of the UnityWebRequest appropriately for the form data specified in the list of IMultipartFormSection objects.
  • This function, by default, attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience - you can use this to check your server’s replies.
  • Similar to the WWWForm POST function, this HLAPI function calls each supplied IMultipartFormSection in turn and formats them into a standard multipart form as specified in RFC 2616.
  • The preformatted form data is stored in a standard UploadHandlerRaw object, which is then attached to the UnityWebRequest. As a result, changes to the IMultipartFormSection objects performed after the UnityWebRequest.POST call are not reflected in the data sent to the server.

Пример

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
 
public class MyBehavior : MonoBehaviour {
    void Start() {
        StartCoroutine(Upload());
    }
 
    IEnumerator Upload() {
        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("http://www.my-server.com/myform", formData);
        yield return www.SendWebRequest();
 
        if(www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error);
        }
        else {
            Debug.Log("Form upload complete!");
        }
    }
}

Using WWWForm (Legacy function)

To help migrate from the WWW system, the UnityWebRequest system permits you to use the old WWWForm object to provide form data.

In this case, the function signature is:

WebRequest.Post(string url, WWWForm formData);

Details

  • This function creates a new UnityWebRequest and sets the target URL to the first string argument’s value. It also reads any custom headers generated by the WWWForm argument (such as Content-Type) and copies them into the UnityWebRequest.
  • This function, by default, attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience - you can use this to check your server’s replies.
  • This function reads the raw data generated by the WWWForm object and buffers it in an UploadHandlerRaw object, which is attached to the UnityWebRequest. Therefore, changes to the WWWForm object after calling UnityWebRequest.POST do not alter the contents of the UnityWebRequest.

Пример

using UnityEngine;
using System.Collections;
 
public class MyBehavior : public MonoBehaviour {
    void Start() {
        StartCoroutine(Upload());
    }
 
    IEnumerator Upload() {
        WWWForm form = new WWWForm();
        form.AddField("myField", "myData");
 
        UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form);
        yield return www.SendWebRequest();
 
        if(www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error);
        }
        else {
            Debug.Log("Form upload complete!");
        }
    }
}
Downloading an AssetBundle from an HTTP server (GET)
Uploading raw data to an HTTP server (PUT)