vedi / restifizer-unity3d

Restifizer SDK for Unity3d
MIT License
62 stars 10 forks source link

Possibility to post, put or patch a normal string (e.g. JSON) instead of an Hashtable #3

Closed Niels-NTG closed 9 years ago

Niels-NTG commented 9 years ago

I seem be one of the many people that have spend hours developing a database with REST-API to be used alongside an Unity app, only to discover that Unity' WWW utility only supports the GET and POST methods. Everything else in my application is ready to go. I already have strings that are nicely formatted in JSON. Restifizer-Unity3D should make me able to use the PUT and PATCH, but makes it impossible for me since it only wants to parse Hashtables, not the JSON strings I have already prepared.

Is it possible to implement something that makes this possible? Or is there a good work-around for my problem?

vedi commented 9 years ago

@Niels-NTG, can you provide an example of the use case, how you see that?

Niels-NTG commented 9 years ago
var jsonString : String;
jsonString = '{"'+"pos"+'": "'+lastSavedPosition.ToString("F2")+'"}';

RestifizerManager.ResourceAt("pos").Patch( jsonString, function(RestifizerResponse){
    // RestifizerResponse.response.Resource;
});

The string (jsonString) get posted to the database "as is", without any conversion.

Currently it is set up like this:

var hashtable : Hashtable = new Hashtable();
hashtable.Add(type, lastSavedPosition.ToString("F2"));

RestifizerManager.ResourceAt("pos").Patch( hashtable, function(RestifizerResponse){
    // RestifizerResponse.response.Resource;
});

My database keeps returning HTTP errorcode 400, meaning that my JSON data is somehow malformed. But if I pass the same string with cURL it works fine.

curl -X PATCH --data '{"pos": "(0.31, 1.13, 0.00)"}' "http://localhost:3000/pos"
vedi commented 9 years ago

We do not serialize Hashtable to JSON in our SDK, we pass it to UnityHTTP (that we use under hood for networking). I see the only workaround for you - parse your JSON to Hashtable, and after that you will be able to work with the SDK with the common way. Definitely it will have some performance impact to your game, but if you do not use this code intensively, it's ok.

Niels-NTG commented 9 years ago

I now have solved the problem by calling UnityHTTP directly, making your library redundant. Apparently I didn't really need Restifizer-Unity3d for my problem.

If you're curious, this is how I solved it:

var encoding = new System.Text.UTF8Encoding();
var theRequest : HTTP.Request = new HTTP.Request("PATCH", url+type+"?id=eq."+playerID, encoding.GetBytes(jsonString) );
theRequest.Send(function(Request){
    // 
});