Skip to content

Instantly share code, notes, and snippets.

@ando-takahiro
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save ando-takahiro/ca60e5da4b2a89808f6a to your computer and use it in GitHub Desktop.

Select an option

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.
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);
}
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