Post
Creates a UnityWebRequest configured to send form data to a server via HTTP POST.
Read time 17 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Networking
- Assembly: UnityEngine.UnityWebRequestModule
Post(string, string, string)
Creates a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(string uri, string postData, string contentType)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send string to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to .
urlurimethodPOSTContent-TypecontentTypeThis 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 will be 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.
postDataExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { using (UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myapi", "{ \"field1\": 1, \"field2\": 2 }", "application/json")) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } } }}
Post(Uri, string, string)
Creates a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(Uri uri, string postData, string contentType)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send string to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to .
urlurimethodPOSTContent-TypecontentTypeThis 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 will be 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.
postDataExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { using (UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myapi", "{ \"field1\": 1, \"field2\": 2 }", "application/json")) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } } }}
Post(string, WWWForm)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(string uri, WWWForm formData)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be copied from the parameter.
urlurimethodPOSTContent-TypeformDataThis 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 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.
formDataExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior2 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData"); using UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(Uri, WWWForm)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(Uri uri, WWWForm formData)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be copied from the parameter.
urlurimethodPOSTContent-TypeformDataThis 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 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.
formDataExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior2 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData"); using UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(string, List<IMultipartFormSection>)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(string uri, List<IMultipartFormSection> multipartFormSections)
Parameters
The target URI to which form data will be transmitted.
A list of form fields or files to be formatted and transmitted to the remote server.
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to , with an appropriate boundary specification.
urlurimethodPOSTContent-Typemultipart/form-dataIf you supply a custom 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.
boundaryThis 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 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.
multipartFormSectionsUsing 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.
Additional Resources: 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.
Examples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior3 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { List<IMultipartFormSection> form = new(); form.Add(new MultipartFormDataSection("myField", "myData")); using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(Uri, List<IMultipartFormSection>)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(Uri uri, List<IMultipartFormSection> multipartFormSections)
Parameters
The target URI to which form data will be transmitted.
A list of form fields or files to be formatted and transmitted to the remote server.
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to , with an appropriate boundary specification.
urlurimethodPOSTContent-Typemultipart/form-dataIf you supply a custom 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.
boundaryThis 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 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.
multipartFormSectionsUsing 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.
Additional Resources: 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.
Examples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior3 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { List<IMultipartFormSection> form = new(); form.Add(new MultipartFormDataSection("myField", "myData")); using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(string, List<IMultipartFormSection>, byte[])
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(string uri, List<IMultipartFormSection> multipartFormSections, byte[] boundary)
Parameters
The target URI to which form data will be transmitted.
A list of form fields or files to be formatted and transmitted to the remote server.
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
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to , with an appropriate boundary specification.
urlurimethodPOSTContent-Typemultipart/form-dataIf you supply a custom 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.
boundaryThis 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 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.
multipartFormSectionsUsing 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.
Additional Resources: 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.
Examples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior3 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { List<IMultipartFormSection> form = new(); form.Add(new MultipartFormDataSection("myField", "myData")); using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(Uri, List<IMultipartFormSection>, byte[])
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(Uri uri, List<IMultipartFormSection> multipartFormSections, byte[] boundary)
Parameters
The target URI to which form data will be transmitted.
A list of form fields or files to be formatted and transmitted to the remote server.
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
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to , with an appropriate boundary specification.
urlurimethodPOSTContent-Typemultipart/form-dataIf you supply a custom 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.
boundaryThis 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 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.
multipartFormSectionsUsing 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.
Additional Resources: 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.
Examples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior3 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { List<IMultipartFormSection> form = new(); form.Add(new MultipartFormDataSection("myField", "myData")); using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(string, Dictionary<string, string>)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(string uri, Dictionary<string, string> formFields)
Parameters
The target URI to which form data will be transmitted.
Strings indicating the keys and values of form fields. Will be automatically formatted into a URL-encoded form body.
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to .
urlurimethodPOSTContent-Typeapplication/x-www-form-urlencodedThe Dictionary of strings in 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, and then joined into a URL-encoded form string. (for example, ).
formFieldskey1=value1&key2=value2This 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 will be converted into a byte stream and stored in an UploadHandlerRaw, which will be attached to this UnityWebRequest.
formFieldsExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior4 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { Dictionary<string, string> form = new(); form["myField"] = "myData"; using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}
Post(Uri, Dictionary<string, string>)
Create a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest Post(Uri uri, Dictionary<string, string> formFields)
Parameters
The target URI to which form data will be transmitted.
Strings indicating the keys and values of form fields. Will be automatically formatted into a URL-encoded form body.
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the to the string argument and sets the to . The header will be set to .
urlurimethodPOSTContent-Typeapplication/x-www-form-urlencodedThe Dictionary of strings in 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, and then joined into a URL-encoded form string. (for example, ).
formFieldskey1=value1&key2=value2This 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 will be converted into a byte stream and stored in an UploadHandlerRaw, which will be attached to this UnityWebRequest.
formFieldsExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;public class MyBehavior4 : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { Dictionary<string, string> form = new(); form["myField"] = "myData"; using UnityWebRequest www = UnityWebRequest.Post("https://httpbin.org/post", form); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } }}