Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity: Use HTTP PUT in Unity3D

I'm quite new to Unity and facing some problems about RESTFul in Unity. I want to update some data on the server by using HTTP PUT, but as what I received when search the web, the WWWW class in Unity doesn't support HTTP PUT. I also tried some HttpWebRequest example related to HTTP PUT but always received error code 400: Bad Request.

How can I solve this problem? Do I have to list out all the key-value pairs when updating or just need to list the pairs I want to change the value ?

like image 433
Brian Pham Avatar asked Apr 16 '15 07:04

Brian Pham


2 Answers

If you're not looking for a 3rd party plugin and assuming your server supports it then one method you could look at using is the "X-HTTP-Method-Override" HTTP header. Your client sends the data to the server via POST, but the server handles this as the value in the X-HTTP-Method-Override header (such as PUT).

I've used this before to great effect where our server supported it. An example of using this in Unity3d would be along the lines of:

string url = "http://yourserver.com/endpoint";
byte[] body = Encoding.UTF8.GetBytes(json);    

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add( "Content-Type", "application/json" );
headers.Add( "X-HTTP-Method-Override", "PUT" );
WWW www = new WWW(url, body, headers);
like image 139
James Woodward Avatar answered Oct 22 '22 05:10

James Woodward


I recommend looking at BestHTTP package instead of default WWW class. It's cheap (almost all Unity3d assets are, compared to typical middleware prices in game industry) and it's pretty decent, judging by personal experience.

Alternatively, you can use standard .NET sockets.

like image 20
Max Yankov Avatar answered Oct 22 '22 03:10

Max Yankov