- Home /
GET Request Wrapper
Howdy folks,
I'm very new to Unity, and struggling with the MonoBehaviour object.
The goal is to create an object such that I can pass in a url, and receive data via GET. Sooo... we ultimately would like something like this:
data = GET.request("http://www.someurl.com/somefile.php?somevariable=somevalue");
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!
I'm sorry, I don't get what you want changed from the WWW approach. Your suggested post seem to do exactly the same, what am I missing from your picture?
Answer by pandemoniumsynd · Jan 22, 2012 at 03:22 AM
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!
Your answer
Follow this Question
Related Questions
How do I post a cURL request from Unity? 1 Answer
PHP POST through $_REQUEST with WEBBUILD 1 Answer
Blank web server response in multiple type data submission 1 Answer
How do I send data over the internet? 0 Answers
HTTP Response Headers? 3 Answers