PostWwwForm
Creates a UnityWebRequest configured to send form data to a server via HTTP POST.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Networking
- Assembly: UnityEngine.UnityWebRequestModule
PostWwwForm(string, string)
Creates a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest PostWwwForm(string uri, string form)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the and sets the method to . The header will be set to by default.
uriPOSTContent-Typeapplication/x-www-form-urlencodedThis 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 string in the parameter is expected to be a preformatted HTML form. It will be escaped and sent as UTF-8 string.
formExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { using (UnityWebRequest www = UnityWebRequest.PostWwwForm("https://www.my-server.com/myapi", "field1=1&field2=value2")) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } } }}
PostWwwForm(Uri, string)
Creates a UnityWebRequest configured to send form data to a server via HTTP .
POSTpublic static UnityWebRequest PostWwwForm(Uri uri, string form)
Parameters
Returns
Type | Description |
|---|---|
| UnityWebRequest | A UnityWebRequest configured to send form data to |
Remarks
This method creates a UnityWebRequest, sets the and sets the method to . The header will be set to by default.
uriPOSTContent-Typeapplication/x-www-form-urlencodedThis 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 string in the parameter is expected to be a preformatted HTML form. It will be escaped and sent as UTF-8 string.
formExamples
using UnityEngine;using UnityEngine.Networking;using System.Collections;public class MyBehavior : MonoBehaviour{ void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { using (UnityWebRequest www = UnityWebRequest.PostWwwForm("https://www.my-server.com/myapi", "field1=1&field2=value2")) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } } }}