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.
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);
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.DownloadHandlerBuffer to the UnityWebRequest. This is for convenience - you can use this to check your server’s replies.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.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 System.Collections;
using Unity.Networking;
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.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}
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);
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.DownloadHandlerBuffer to the UnityWebRequest. This is for convenience - you can use this to check your server’s replies.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;
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.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}