Version: 5.6 (switch to 2017.1b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

UnityWebRequest.Post

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Post(uri: string, postData: string): Networking.UnityWebRequest;
public static Networking.UnityWebRequest Post(string uri, string postData);

Parameters

uri The target URI to which form data will be transmitted.
postData Form body data. Will be URLEncoded via WWWTranscoder.URLEncode prior to transmission.

Returns

UnityWebRequest A UnityWebRequest configured to send form data to uri via POST.

Description

Create a UnityWebRequest configured to send form data to a server via HTTP POST.

This method creates a UnityWebRequest, sets the url to the string uri argument and sets the method to POST. The Content-Type header will be set to application/x-www-form-urlencoded by default.

Note: Many server backend languages do not properly handle POST requests with Content-Type headers set to encoding others than application/x-www-form-urlencoded or multipart/form-data.

This method attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience, as we anticipate most users will use the DownloadHandler to check replies from the server, particularly in the case of REST APIs.

The data in postData will be escaped via WWWTranscoder.URLEncode, then interpreted into a byte stream via System.Text.Encoding.UTF8. The resulting byte stream will be stored in an UploadHandlerRaw and the Upload Handler will be attached to this UnityWebRequest.

no example available in JavaScript
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

class MyBehavior : public MonoBehaviour { void Start() { StartCoroutine(Upload()); }

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

using (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!"); } } } }

public static function Post(uri: string, formData: WWWForm): Networking.UnityWebRequest;
public static Networking.UnityWebRequest Post(string uri, WWWForm formData);

Parameters

uri The target URI to which form data will be transmitted.
formData Form fields or files encapsulated in a WWWForm object, for formatting and transmission to the remote server.

Returns

UnityWebRequest A UnityWebRequest configured to send form data to uri via POST.

Description

Create a UnityWebRequest configured to send form data to a server via HTTP POST.

This method creates a UnityWebRequest, sets the url to the string uri argument and sets the method to POST. The Content-Type header will be copied from the formData parameter.

This method attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience, as we anticipate most users will use the DownloadHandler to check replies from the server, particularly in the case of REST APIs.

The formData object will generate an appropriately-formatted byte stream, depending on its contents. The resulting byte stream will be stored in an UploadHandlerRaw and the Upload Handler will be attached to this UnityWebRequest.

no example available in JavaScript
using UnityEngine;
using UnityEngine.Networking;
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!"); } } }

public static function Post(uri: string, multipartFormSections: List<IMultipartFormSection>): Networking.UnityWebRequest;
public static Networking.UnityWebRequest Post(string uri, List<IMultipartFormSection> multipartFormSections);
public static function Post(uri: string, multipartFormSections: List<IMultipartFormSection>, boundary: byte[]): Networking.UnityWebRequest;
public static Networking.UnityWebRequest Post(string uri, List<IMultipartFormSection> multipartFormSections, byte[] boundary);

Parameters

uri The target URI to which form data will be transmitted.
multipartFormSections A list of form fields or files to be formatted and transmitted to the remote server.
boundary A unique boundary string, which will be used when separating form fields in a multipart form. If not supplied, a boundary will be generated for you.

Returns

UnityWebRequest A UnityWebRequest configured to send form data to uri via POST.

Description

Create a UnityWebRequest configured to send form data to a server via HTTP POST.

This method creates a UnityWebRequest, sets the url to the string uri argument and sets the method to POST. The Content-Type header will be set to multipart/form-data, with an appropriate boundary specification.

If you supply a custom boundary byte array, note that the sequence of bytes must be guaranteed to be unique and must not appear anywhere in the body of your form data. For more information on multipart forms and form boundaries, see RFC 2388.

This method attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience, as we anticipate most users will use the DownloadHandler to check replies from the server, particularly in the case of REST APIs.

The List of IMultipartFormSection objects in multipartFormSections will be formatted into a valid multipart form body. Each object will be interpreted as a discrete form section. The byte stream resulting from formatting this multipart form body will be stored in an UploadHandlerRaw and attached to this UnityWebRequest.

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.

See Also: MultipartFormDataSection and MultipartFormFileSection.

A List of IMultipartFormSection objects can be provided to this method. The list's members will be formatted into a multipart form, as defined by RFC 2388.

Multipart forms require a unique boundary string to define the separation between fields. The boundary string must be guaranteed to not be present anywhere within the body of any form field in the request. If you do not supply a boundary, Unity will generate one. The generated boundary is 40 random printable bytes, which effectively never collide with form field data. However, if your application requires you to supply a custom boundary string, you may do so.

The supplied boundary, if any, will be automatically converted from a byte array to UTF8 characters.

no example available in JavaScript
using UnityEngine;
using UnityEngine.Networking;
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!"); } } }

public static function Post(uri: string, formFields: Dictionary<string,string>): Networking.UnityWebRequest;
public static Networking.UnityWebRequest Post(string uri, Dictionary<string,string> formFields);

Parameters

uri The target URI to which form data will be transmitted.
formFields Strings indicating the keys and values of form fields. Will be automatically formatted into a URL-encoded form body.

Returns

UnityWebRequest A UnityWebRequest configured to send form data to uri via POST.

Description

Create a UnityWebRequest configured to send form data to a server via HTTP POST.

This method creates a UnityWebRequest, sets the url to the string uri argument and sets the method to POST. The Content-Type header will be set to application/x-www-form-urlencoded.

The Dictionary of strings in formFields will be interpreted as a list of form fields whose field IDs are the dictionary keys, and whose field values are the dictionary values. Both keys and values will be escaped via WWWTranscoder.URLEncode, and then joined into a URL-encoded form string. (e.g. key1=value1&key2=value2).

This method, by default, attaches a DownloadHandlerBuffer to the UnityWebRequest. This is for convenience, as we anticipate most users will use the DownloadHandler to check replies from the server, particularly in the case of REST APIs.

The URL-encoded form string generated from formFields will be converted into a byte stream and stored in an UploadHandlerRaw, which will be attached to this UnityWebRequest.

no example available in JavaScript
using UnityEngine;
using UnityEngine.Networking;
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!"); } } }