0

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 = "my.ip.address.here/connections";

    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.

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
John
  • 1,440
  • 1
  • 11
  • 18
  • WWW is only used for POST/GET requests. UnityWebRequest should be used for this. – Programmer Nov 26 '16 at 00:42
  • I don't think that's it- I'm having the same issue making GET requests. – John Nov 26 '16 at 00:45
  • The code in your question shows that you are trying to make DELETE request with the WWW class. Again, you can't do that. You have to use UnityWebRequest. If you try UnityWebRequest and fail, make a new question with your new code. – Programmer Nov 26 '16 at 00:50
  • 1
    The problem was that you need to use StartCoroutine to trigger a coroutine. In the Start should be StartCoroutine(RESTHelper(SERVER, "", "DELETE");); – Everts Nov 26 '16 at 07:03

0 Answers0