A simple REST client for Unity3D with fluent interface. Unlike other clients, this client works both in the editor and in the build. and it requires EditorCoroutine package to work in the editor.
The easiest way to install OpenRest is using UPM:
openupm add com.omid3098.openrest
Or install manually:
- Install Newtonsoft.Json package from the Package Manager window -> Add package by name:
com.unity.nuget.newtonsoft-json
-
Install EditorCoroutine package from the Package Manager window
-
Copy Contents of the Packages folder into your project
Packages/com.omid3098.openrest
// Add required namespaces
using OpenRest;
// Create a rest client
var client = RestClient.Get()
.Url("https://jsonplaceholder.typicode.com/posts/1")
.OnSuccess((response) => {
Debug.Log(response);
}).Send();
JObject jsonObject = new JObject(){
{"title", "foo"},
{"body", "bar"},
{"userId", 1}
};
RestClient.Post()
.Url("https://jsonplaceholder.typicode.com/posts/1")
.Headers(new Dictionary<string, string> {
{ "Content-Type", "application/json" },
{ "Authorization", "Bearer " + API_KEY}
}).JsonData(
jsonObject.ToString(Formatting.None)
).OnSuccess((response) =>
{
Debug.Log(response);
}).OnProgress((progress) =>
{
Debug.Log(progress);
}).OnError((error) =>
{
Debug.Log(error);
}).Send();