Restclient For Unity : Supported Platforms
Restclient For Unity : Supported Platforms
Supported platforms
The UnityWebRequest system supports most Unity platforms:
Unity package
Download and install the .unitypackage file of the latest release published here.
Nuget package
Other option is downloading this package from NuGet with Visual Studio or using the nuget-cli,
a NuGet.config file is required at the root of your Unity Project, for example:
Getting Started 📚
The default methods (GET, POST, PUT, DELETE, HEAD) are:
RestClient.Get("https://jsonplaceholder.typicode.com/posts/1").Then(res => {
EditorUtility.DisplayDialog("Response", res.Text, "Ok");
});
RestClient.Post("https://jsonplaceholder.typicode.com/posts", newPost).Then(res => {
EditorUtility.DisplayDialog("Status", res.StatusCode.ToString(), "Ok");
});
RestClient.Put("https://jsonplaceholder.typicode.com/posts/1", updatedPost).Then(res => {
EditorUtility.DisplayDialog("Status", res.StatusCode.ToString(), "Ok");
});
RestClient.Delete("https://jsonplaceholder.typicode.com/posts/1").Then(res => {
EditorUtility.DisplayDialog("Status", res.StatusCode.ToString(), "Ok");
});
RestClient.Head("https://jsonplaceholder.typicode.com/posts").Then(res => {
EditorUtility.DisplayDialog("Status", res.StatusCode.ToString(), "Ok");
});
With all the methods we have the possibility to indicate the type of response, in the following
example we're going to create a class and the HTTP requests to load JSON data easily:
[Serializable]
public class User
{
public int id;
public string name;
public string username;
public string email;
public string phone;
public string website;
}
• GET JSON
RestClient.GetArray<User>(usersRoute).Then(users => {
EditorUtility.DisplayDialog("Array", JsonHelper.ArrayToJsonString<User>(users, true), "Ok");
});
[Serializable]
public class CustomResponse
{
public int id;
}
• POST
• PUT
Also we can add specific options and override default headers for a request
And later we can clean the default headers for all requests
RestClient.CleanDefaultHeaders();
Full Example
• Unity
[Serializable]
public class ServerResponse {
public string id;
public string date; //DateTime is not supported by JsonUtility
}
[Serializable]
public class User {
public string firstName;
public string lastName;
}
RestClient.Post<ServerResponse>("www.api.com/endpoint", new User {
firstName = "Juan David",
lastName = "Nicholls Cardona"
}).Then(response => {
EditorUtility.DisplayDialog("ID: ", response.id, "Ok");
EditorUtility.DisplayDialog("Date: ", response.date, "Ok");
});
Collaborators 🥇
Supporting 🍻
I believe in Unicorns 🦄 Support me, if you do too.
Happy coding 💯
Made with ❤️