- Home /
RESTful libraries for Unity
What is the simplest way to interact with a RESTful web service that deals in JSON? I see that WWWForm will let me GET and POST to a web service and that I could use litJSON to encode and parse my data, but litJSON doesn't seem to have been maintained in a while.
How do Unity developers usually do this? Is there a Unity analogue of Resty for Objective-C?
I'm still looking for HTTP classes... in terms of Json, SimpleJSON (http://wiki.unity3d.com/index.php/SimpleJSON) worked for me.
Answer by Deepscorn · Feb 22, 2017 at 08:55 PM
Update: Now, it's better to use unity's UnityWebRequest for requests. For simple serialization you can use unity's JsonUtility. Though, if you want incapsulation, you may want accessors (public get, private set). Then you can choose something from the web. There are plenty of serializers. Newtonsoft Json and https://github.com/neuecc/Utf8Json can do. Not tried them though, still have to maintain legacy codebase with legacy simplejson serializer.
Below is my original answer, written in 2017:
I am using RestSharp with success for android, iOs, Windows, Mac OS platforms. There is a fork of RestSharp which fixes some issues so that RestSharp works for Unity https://github.com/eamonwoortman/RestSharp.Unity. It's cool because you can use DesirializeAs attibute and use any names for fields, even with spaces. Example:
public class PurchasedProductResponse
{
[DeserializeAs(Name = "product id")]
public string ProductId { get; set; }
[DeserializeAs(Name = "rewards")]
public List<MoneyPrizeResponse> MoneyPrizes { get; set; }
}
public class MoneyPrizeResponse
{
[DeserializeAs(Name = "bonusData.imageUrl")]
public string ImageUrl { get; set; }
}
It's cool, Unity added JsonUtility to work with jsons, but it still lacks such feature
Also, I used SimpleJson. It's for serializing/deserializing only. It also supports aliasing via DataContract (enables through SI$$anonymous$$PLE_JSON_DATACONTRACT define). https://github.com/facebook-csharp-sdk/simple-json/blob/master/src/SimpleJson/SimpleJson.cs
Yea, there is a client generator, the problem is that it doesn't work in WebGL and has several other limitations. Please take a look at https://github.com/kolodi/UnityOpenApi - Unity friendly restful API client generator.
Answer by fedor.shubin · Jan 28, 2015 at 03:05 PM
We've created our REST SDK on the shoulders of UnityHTTP. It provides API similar to restangular, implements path building, error handling, and other useful things. You can find it here: https://github.com/vedi/restifizer-unity3d
For example it allows something like that:
RestifizerManager.ResourceAt("api/users").
WithBearerAuth().
One(userId).
Patch(parameters, (response) => {
// response.Resource - at this point you can use the result
});
What is important to note is that it depends on https://github.com/andyburke/UnityHTTP which uses the GPL license, which means when you distribute your Unity application you must make the source code public.
Answer by misher · Mar 08, 2019 at 10:54 AM
I've been working on Unity-specific Restful API client generator https://github.com/kolodi/UnityOpenApi, it's based on Swagger/OpenAPI standard and use convenient Unity tools and approaches.
Answer by raybarrera · Oct 21, 2013 at 10:52 PM
I've used JsonFx, which has worked great. Get it here
As for talking to the web service, you could go with WWW, but be ware that it does not support headers for GET requests. You could write your own solution using sockets, or do what I did and grab uniweb. Hope that helps.
Answer by jimunwin · Aug 20, 2014 at 05:31 AM
I've been using UnityHTTP, which has been super simple to set up and gives me the "PUT" verb, though it hasn't been without its problems. Your code ends up looking something like this:
HTTP.Request theRequest = new HTTP.Request( "PUT", myUrl, lightSettings ); // build the request
theRequest.Send(); // send the request
Where "myUrl" is a string and "lightSettings" is a hashtable.
Your answer
Follow this Question
Related Questions
I cant get response json from rest call in Unity 1 Answer
JSON-Serializer (Renewed open) 0 Answers
Set Content-Length header for UnityWebRequest POST requests in 2017.3? 1 Answer
How to run a human avatar from json response from webservice? 1 Answer
Create a glossary using Simple JSON, loading data dynamically 1 Answer