Last active
July 8, 2020 10:52
-
-
Save arkarmintun1/9362040b17ee23608811549bd07faf3e to your computer and use it in GitHub Desktop.
Unity Web Request Get, Post, Put, Delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original Source: https://stackoverflow.com/a/46008025/9389474 | |
// Get Request | |
void Start() | |
{ | |
StartCoroutine(getRequest("http:///www.yoururl.com")); | |
} | |
IEnumerator GetRequest(string uri) | |
{ | |
UnityWebRequest uwr = UnityWebRequest.Get(uri); | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + uwr.downloadHandler.text); | |
} | |
} | |
// Post Request | |
void Start() | |
{ | |
StartCoroutine(postRequest("http:///www.yoururl.com")); | |
} | |
IEnumerator PostRequest(string url) | |
{ | |
WWWForm form = new WWWForm(); | |
form.AddField("myField", "myData"); | |
form.AddField("Game Name", "Mario Kart"); | |
UnityWebRequest uwr = UnityWebRequest.Post(url, form); | |
uwr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + uwr.downloadHandler.text); | |
} | |
} | |
// Post Request with Json | |
void Start() | |
{ | |
StartCoroutine(postRequest("http:///www.yoururl.com", "your json")); | |
} | |
IEnumerator PostRequest(string url, string json) | |
{ | |
var uwr = new UnityWebRequest(url, "POST"); | |
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json); | |
uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend); | |
uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); | |
uwr.SetRequestHeader("Content-Type", "application/json"); | |
//Send the request then wait here until it returns | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + uwr.downloadHandler.text); | |
} | |
} | |
// Post Request with Multipart Form | |
void Start() | |
{ | |
StartCoroutine(postRequest("http:///www.yoururl.com")); | |
} | |
IEnumerator PostRequest(string url) | |
{ | |
List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); | |
formData.Add(new MultipartFormDataSection("field1=foo&field2=bar")); | |
formData.Add(new MultipartFormFileSection("my file data", "myfile.txt")); | |
UnityWebRequest uwr = UnityWebRequest.Post(url, formData); | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + uwr.downloadHandler.text); | |
} | |
} | |
// Put Request | |
void Start() | |
{ | |
StartCoroutine(putRequest("http:///www.yoururl.com")); | |
} | |
IEnumerator PutRequest(string url) | |
{ | |
byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test"); | |
UnityWebRequest uwr = UnityWebRequest.Put(url, dataToPut); | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + uwr.downloadHandler.text); | |
} | |
} | |
// Delete Request | |
void Start() | |
{ | |
StartCoroutine(deleteRequest("http:///www.yoururl.com")); | |
} | |
IEnumerator DeleteRequest(string url) | |
{ | |
UnityWebRequest uwr = UnityWebRequest.Delete(url); | |
yield return uwr.SendWebRequest(); | |
if (uwr.isNetworkError) | |
{ | |
Debug.Log("Error While Sending: " + uwr.error); | |
} | |
else | |
{ | |
Debug.Log("Deleted"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment