Last active
August 29, 2015 14:06
-
-
Save ando-takahiro/ca60e5da4b2a89808f6a to your computer and use it in GitHub Desktop.
node.js style REST client function for unity3d. paste bellow into your MonoBehaviour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected delegate void OnResponse(string error, string body); | |
| protected void Post(string url, Dictionary<string, string> options, OnResponse onResponse) | |
| { | |
| StartCoroutine(PostFlow(url, options, onResponse)); | |
| } | |
| protected IEnumerator PostFlow(string url, Dictionary<string, string> options, OnResponse onResponse) | |
| { | |
| if (options == null) | |
| { | |
| options = new Dictionary<string, string>(); | |
| } | |
| var form = new WWWForm(); | |
| foreach (var pair in options) | |
| { | |
| form.AddField(pair.Key, pair.Value); | |
| } | |
| var www = new WWW(url, form); | |
| yield return www; | |
| string body = ""; | |
| if (string.IsNullOrEmpty(www.error)) | |
| { | |
| body = www.text; | |
| } | |
| onResponse(www.error, body); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Post("http://hoge", new Dictionary<string, string>{{"key", "value"}, {"key2", "value2"}}, (error, body) => { | |
| // hogehoge | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment