17

This is a Unity3d in C# question. The goal is to create an object such that I can pass in a URL and receive data via GET, an object that I would create the would be a wrapper for the WWW logic. I would also like a 'POST' object too, where I could supply a url and a 'Dictionary' of key-value pairs as the post arguements. Sooo... we ultimately would like something like this:

get_data = GET.request("http://www.someurl.com/somefile.php?somevariable=somevalue");

AND

post_data = POST.request("http://www.someurl.com/somefile.php", post)
// Where post is a Dictionary of key-value pairs of my post arguments. 

To try and accomplish this, I use the WWW object. Now, in order to give the WWW object time to download, we need to have this happening inside a MonoBehaviour object and yield the results. So I got this, which works:

public class main : MonoBehavior
{
    IEnumerator Start()
    {
        WWW www = new WWW("http://www.someurl.com/blah.php?action=awesome_stuff"); 
        yield return www;
        Debug.Log(www.text);
    }
}

What I really want is this:

public class main : MonoBehavior
{
    IEnumerator Start()
    {
        GET request = new GET("http://www.someurl.com/blah.php?action=awesome_stuff"); 
        Debug.Log(request.get_data()); // Where get_data() returns the data (which will be text) from the request.   
    }
}

Now I have the main script attached to the single GameObject in the hierarchy (called root). Do I need to have the GET script attached to the root GameObject as well? Can I do that dynamically from main?

Ultimately, I need a solution that allows me to easily send GET and POST requests.

Cheers!

3 Answers 3

17

Ah, Got it!

My problem was a misunderstanding of how MonoBehaviour and Coroutines worked. The solution is very simple.

In the editor, make an empty GameObject. I named it DB. Then attach the following script to it:

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
class DB : MonoBehaviour
{
    void Start() { }

    public WWW GET(string url)
    {
        WWW www = new WWW(url);
        StartCoroutine(WaitForRequest(www));
        return www;
    }

    public WWW POST(string url, Dictionary<string, string> post)
    {
        WWWForm form = new WWWForm();
        foreach (KeyValuePair<String, String> post_arg in post)
        {
            form.AddField(post_arg.Key, post_arg.Value);
        }
        WWW www = new WWW(url, form);

        StartCoroutine(WaitForRequest(www));
        return www;
    }

    private IEnumerator WaitForRequest(WWW www)
    {
        yield return www;

        // check for errors
        if (www.error == null)
        {
            Debug.Log("WWW Ok!: " + www.text);
        }
        else
        {
            Debug.Log("WWW Error: " + www.error);
        }
    }
}

Then, in your main script's start function, you can do this!

private DB db;
void Start()
{
    db = GameObject.Find("DB").GetComponentInChildren<DB>();
    results = db.GET("http://www.somesite.com/someAPI.php?someaction=AWESOME");
    Debug.Log(results.text);
}

Haven't tested the POST requests, but now all the logic is wrapped up! Send HTTP requests to your hearts desire, cheers!

7
  • 1
    big issue though in using the FIND method is that you will have a big hit on your game performance.... trying to find a game object.... check out nice link on how to work with delegates unifycommunity.com/wiki/… Commented Jan 26, 2012 at 7:25
  • Absolutely correct. Ideally you would keep track of the object via reference variable, and hang onto it so you only need do that once. Or if a gameobject in the scene is using this wrapper, you could have a public var which could be assigned the DB GO in the Unity editor, then you need not find anything! Commented Jan 15, 2013 at 17:26
  • 2
    I don't think this works actually. When you are doing: StartCoroutine(WaitForRequest(request)) Debug.Log(request.text) If the request is not done yet only the coroutine will be stopped, not the actual function. Which Debug.Log(request.text) will be called before the request has finished. Commented Nov 18, 2013 at 16:00
  • Tiberiu is right. You need to use a callback function.
    – Tom
    Commented Jun 28, 2015 at 5:25
  • 1
    @ThePoet It's defined right below the POST function, with little messages saying whether or not the request completed. Commented Dec 1, 2015 at 22:55
2

What is that GET script that you're referring to? The WWW class allows you to retrieve GET data just fine, the information you need is in the text property of the instantiated WWW object. Here's the documentation:

http://unity3d.com/support/documentation/ScriptReference/WWW-text.html http://unity3d.com/support/documentation/ScriptReference/WWW.html

All you need to do is yield the WWW object, as you're doing just now, and then read any of the properties you're interested in, plain and simple, no extra classes needed.

As for sending a POST object, that's what the WWWForm class is for:

http://unity3d.com/support/documentation/ScriptReference/WWWForm.html

In short, you just create a WWWForm object, add fields to it through AddField(), and then simply construct a new WWW object with the POST URL & the former object, that's it. Yield the WWW object and once it comes back you're data has been submitted. Responses are, again, in the text property & errors in the corresponding field. Plain, clean & simple.

HTH!

1
  • Sorry, the GET object is class that wraps up the logic of WWW. The advantage being all I need to do would be to supply a URL and receive an output. I'm not sure if that's possible given how unity works however. I'll edit my question. Commented Jan 21, 2012 at 17:34
0

Here is @pandemoniumsyndicate's code modified to add a callback. The original code is not completely correct, since the GET and POST functions will exit immediately after calling the coroutine. At that time it is likely that the WWW request is not yet complete and accessing any field except (www.isDone) is pointless.

The following code defines a delegate, WWWRequestFinished, which will be called when the request is finished with the result of the request and data received, if any.

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WWWRequestor : MonoBehaviour 
{

    Dictionary<WWW, object> mRequestData = new Dictionary<WWW, object>();
    public delegate void WWWRequestFinished(string pSuccess, string pData);

    void Start() { }

    public WWW GET(string url, WWWRequestFinished pDelegate)
    {
        WWW aWww = new WWW(url);
        mRequestData[aWww] = pDelegate;

        StartCoroutine(WaitForRequest(aWww));
        return aWww;
    }

    public WWW POST(string url, Dictionary<string, string> post, WWWRequestFinished pDelegate)
    {
        WWWForm aForm = new WWWForm();
        foreach (KeyValuePair<String, String> post_arg in post)
        {
            aForm.AddField(post_arg.Key, post_arg.Value);
        }
        WWW aWww = new WWW(url, aForm);

        mRequestData[aWww] = pDelegate;
        StartCoroutine(WaitForRequest(aWww));
        return aWww;
    }

    private IEnumerator WaitForRequest(WWW pWww)
    {
        yield return pWww;

        // check for errors
        string aSuccess = "success";
        if (pWww.error != null)
        {
            aSuccess = pWww.error;
        }

        WWWRequestFinished aDelegate = (WWWRequestFinished) mRequestData[pWww];
        aDelegate(aSuccess, pWww.text);
        mRequestData.Remove(pWww);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.