A TcpClient-based HTTP library for Unity.
Find a file
Andy Burke 58d043b098 Merge pull request #69 from zlumer/master
Ability to change logging behavior
2017-09-12 18:47:30 -07:00
lib Move to compact version of Ionic.Zlib 2015-06-16 11:54:57 +03:00
src added DiscardLogger 2017-09-08 17:21:30 +03:00
.gitignore I have added a nuspec file and am working on getting this project into NuGet. I have not yet tested. #25 2014-12-06 12:09:02 -04:00
AssemblyInfo.cs I have updated the renamed external to libs because this makes it more clear these are external libraries. I have also added an AssemblyInfo.cs though have not populated. The missing AssemblyInfo.cs was the first reason I was not able to build this using Mono Develop. I have moved the source files to a src directory rather than leaving them top level. I have also now excluded the bin directory from the repo because these files can be generated locally. Any files that cannot be generate locally and are used as tools should be added to a directory called tools. I have also updated the Ionic.Zlib dll to the latest version to allow it to be compiled with more up to date version of .NET / Mono. 2014-12-01 15:55:08 -04:00
LICENSE.md Add a license which closes #29 2014-12-07 17:36:17 -04:00
README.md Add license to README 2015-06-22 15:06:58 -07:00
UnityHTTP.csproj I have added a nuspec file and am working on getting this project into NuGet. I have not yet tested. #25 2014-12-06 12:09:02 -04:00
UnityHTTP.nuspec Add a license which closes #29 2014-12-07 17:36:17 -04:00

Attribution

Based on Simon Wittber's UnityWeb code (http://code.google.com/p/unityweb/).

LICENSE

UnityHTTP falls under the GPL due to its basis on Simon Wittber's UnityWeb code, which is licensed under the GPL.

You should be aware of this license and determine if it is acceptable for your project.

About

This is a TcpClient-based HTTP library for use in Unity. It should work in both the standalone player and in the web player.

It also has convenience methods for working with JSON.

Examples

IEnumerator example:

public IEnumerator SomeRoutine() {
    HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
    someRequest.Send();

    while( !someRequest.isDone )
    {
        yield return null;
    }

    // parse some JSON, for example:
    JSONObject thing = new JSONObject( request.response.Text );
}

Closure-style (does not need to be in a coroutine):

HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
someRequest.Send( ( request ) => {
    // parse some JSON, for example:
    JSONObject thing = new JSONObject( request.response.Text );
});

Post request using form data:

WWWForm form = new WWWForm();
form.AddField( "something", "yo" );
form.AddField( "otherthing", "hey" );

HTTP.Request someRequest = new HTTP.Request( "post", "http://someurl.com/some/post/handler", form );
someRequest.Send( ( request ) => {
    // parse some JSON, for example:
    bool result = false;
    Hashtable thing = (Hashtable)JSON.JsonDecode( request.response.Text, ref result );
    if ( !result )
    {
        Debug.LogWarning( "Could not parse JSON response!" );
        return;
    }
});

Post request using JSON:

Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );

// When you pass a Hashtable as the third argument, we assume you want it send as JSON-encoded
// data.  We'll encode it to JSON for you and set the Content-Type header to application/json
HTTP.Request theRequest = new HTTP.Request( "post", "http://someurl.com/a/json/post/handler", data );
theRequest.Send( ( request ) => {

    // we provide Object and Array convenience methods that attempt to parse the response as JSON
    // if the response cannot be parsed, we will return null
    // note that if you want to send json that isn't either an object ({...}) or an array ([...])
    // that you should use JSON.JsonDecode directly on the response.Text, Object and Array are
    // only provided for convenience
    Hashtable result = request.response.Object;
    if ( result == null )
    {
        Debug.LogWarning( "Could not parse JSON response!" );
        return;
    }
  
});

If you want to make a request while not in Play Mode (e. g. from a custom Editor menu command or wizard), you must use the Request synchronously, since Unity's main update loop is not running. The call will block until the response is available.

Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );

HTTP.Request theRequest = new HTTP.Request("post", "http://someurl.com/a/json/post/handler", data );
theRequest.synchronous = true;
theRequest.Send((request) => {
	EditorUtility.DisplayDialog("Request was posted.", request.response.Text, "Ok");
});