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 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!");
}
}
}
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;
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!");
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!