Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pandemoniumsynd · Jan 21, 2012 at 04:35 PM · wwwwwwformgetpostmonobehavior

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Jan 21, 2012 at 06:28 PM 0
Share

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?

avatar image Statement · Jan 21, 2012 at 06:28 PM 0
Share

Is it that you want it to be a blocking call?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges