vendredi 25 novembre 2016

How do I make a RESTful call in Unity with WWW?

I've got a Unity game that I'm working on which needs to make calls to a RESTful API (meaning that I need to make GET, POST, PUT, and DELETE calls). I'm trying to use the WWW class to make these calls as shown below, but it's not working:

public class GameRegistry : MonoBehaviour { public string SERVER = "http://ift.tt/2fApgvT";

private static string METHOD_HEADER = "X-HTTP-Method-Override";
private static string CONTENT_HEADER = "content-type";
private static string CONTENT_TYPE = "application/json";

public void Start()
{
    Debug.Log("Starting...");
    RESTHelper(SERVER, "", "DELETE");
    Debug.Log("...Finishing.");
}

private IEnumerator<WWW> RESTHelper(string url, string payload, string method)
{
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers.Add(METHOD_HEADER, method);
    headers.Add(CONTENT_HEADER, CONTENT_TYPE);

    Debug.Log("Request sent...");
    WWW request = new WWW(url, Encoding.UTF8.GetBytes(payload), headers);
    yield return request;
    Debug.Log("...reponse received.");
    Debug.Log(request.text);
}

When I attach this to a GameObject and run it in my game, I get the following output:

Starting...
...Finishing.

Nothing appears to be executed in the helper method. What am I doing wrong?

FWIW, I have tested the API with ARC and it works perfectly.




Aucun commentaire:

Enregistrer un commentaire