Making a REST call from a Unity Project

KonfuPanda picture KonfuPanda · May 18, 2015 · Viewed 7.3k times · Source

I need to control a robot from within Unity3D. The robot responds to these requests:

class r0.server.httpserver.HTTPServer(*args, **kwargs)
Bases: pypot.server.httpserver.HTTPServer

Bottle based HTTPServer used to remote access a robot.
The server answers to the following requests:

  • GET /motor/list.json
  • GET /primitive/list.json
  • GET /motor/<name>/register.json (or GET /<name>/register.json) etc.

So this is an example of the documentation. I found this code example to make a REST call using C#:

 static string HttpGet(string url)
    {
        HttpWebRequest req = WebRequest.Create(url)
                             as HttpWebRequest;
        string result = null;
        using (HttpWebResponse resp = req.GetResponse()
                                      as HttpWebResponse)
        {
            StreamReader reader =
                new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
        return result;
    }

(http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html)

So if I understood this right, I have to cast the response in json format. But how do I make the right GET call as there are many different ones. The static IP adress of the robot is 192.168.200.99, is this the URL I need to use for the requests? Thx for help

Answer

WellerEE picture WellerEE · May 18, 2015

Let me suggest that you get Postman to help you determine the correct url and test your API calls.

Not knowing truly how your robot's API works, we must assume that your robot does not have a domain name. As such, the IP address should work. Using the provided IP address for the base url would translate as:

http://{base}/motor/list.json -> http://192.168.200.99/motor/list.json

Again Postman can really make discovery a lot easier.

Here is an image showing a screen shot of Postman doing a get for the OpenWeatherMap api.

Postman in Action