Last active
March 18, 2024 08:46
-
-
Save pferreirafabricio/57eea7a719a2c34ca8b30dacacdd8eb1 to your computer and use it in GitHub Desktop.
A simple C# script for making HTTP Request on Unity
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
using System; | |
using System.Collections; | |
using System.Text; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
namespace Project.Base | |
{ | |
public static class Request | |
{ | |
public static IEnumerator post(string routeName, dynamic data, Action<Response> handleResponse) | |
{ | |
using (UnityWebRequest request = new UnityWebRequest($"http://localhost:8000/{handleRouteName(routeName)}", "POST")) | |
{ | |
string jsonFields = JsonUtility.ToJson(data); | |
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonFields)); | |
request.downloadHandler = new DownloadHandlerBuffer(); | |
request.SetRequestHeader("Content-Type", "application/json"); | |
request.SetRequestHeader("Accept", "application/json"); | |
yield return request.SendWebRequest(); | |
Response apiReponse = JsonUtility.FromJson<Response>(request.downloadHandler?.text); | |
handleResponse(apiReponse); | |
} | |
} | |
public static IEnumerator get(string routeName, Action<Response> handleResponse) | |
{ | |
using (UnityWebRequest request = new UnityWebRequest($"http://localhost:8000/{handleRouteName(routeName)}", "GET")) | |
{ | |
request.downloadHandler = new DownloadHandlerBuffer(); | |
request.SetRequestHeader("Content-Type", "application/json"); | |
request.SetRequestHeader("Accept", "application/json"); | |
yield return request.SendWebRequest(); | |
Response apiReponse = JsonUtility.FromJson<Response>(request.downloadHandler?.text); | |
handleResponse(apiReponse); | |
} | |
} | |
public static IEnumerator put(string routeName, dynamic data, Action<Response> handleResponse = null) | |
{ | |
using (UnityWebRequest request = new UnityWebRequest($"http://localhost:8000/{handleRouteName(routeName)}", "PUT")) | |
{ | |
string jsonFields = JsonUtility.ToJson(data); | |
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonFields)); | |
request.downloadHandler = new DownloadHandlerBuffer(); | |
request.SetRequestHeader("Content-Type", "application/json"); | |
request.SetRequestHeader("Accept", "application/json"); | |
yield return request.SendWebRequest(); | |
Response apiReponse = JsonUtility.FromJson<Response>(request.downloadHandler?.text); | |
if (handleResponse != null) | |
handleResponse(apiReponse); | |
} | |
} | |
private static string handleRouteName(string routeName) | |
{ | |
if (routeName.StartsWith("/")) | |
return routeName; | |
return $"/{routeName}"; | |
} | |
} | |
} |
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
using System; | |
namespace Project.Base | |
{ | |
[Serializable] | |
public class Response | |
{ | |
public Data data; | |
} | |
[Serializable] | |
public class Data | |
{ | |
public string error; | |
public string message; | |
public Player player; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment