- Home /
Accessing RESTful web apis from unity
Is it possible to get the WWW class to use any other http verb than GET or POST? I'm trying to access web apis wich relies on the correct the http verb for an operation.
Most of the time my apps will be running in the web player, so I guess I could create some javascript functions exposing the jquery $.ajax() or similar and call the javascript from unity, but that excludes debugging in the editor.
You may be interested in this Unity-specific Restful API client generator https://github.com/kolodi/UnityOpenApi
Answer by KvanteTore · Mar 29, 2010 at 10:04 PM
Silly me, I just realized I can use the System.Net.HttpWebRequest
, it provides me with all the http-goodness I care for.
Oh, that's all you wanted to know? I guess I read into the question a bit more than I thought... people here tend to ask more complicated questions than they really intend to, heh. ;)
:) I might have spoken out too soon though. I'm having a some problems actually getting HttpWebRequest to perform requests from within Unity
never$$anonymous$$d. all is working well with the HttpWebRequest :)
never$$anonymous$$d the never$$anonymous$$d. It turns out that HttpWebRequest is not available in the webplayer :(
webplayer is now deprecated, UnityWebRequest is working in WebGL builds. In fact, it is what I'm using in https://github.com/kolodi/UnityOpenApi - unity friendly restful client generator
Uniweb might work with the webplayer, though I've never tried it personally. I have used uniweb for mobile and desktop and it has worked for my needs.
Answer by Lowgain · Feb 03, 2012 at 01:23 AM
"The HTTP protocol only supports the GET and POST operations" <- not true
Not sure where you copy pasted that from, but it's not true. http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
Answer by Mike McFarland · Mar 29, 2013 at 01:58 PM
Even though this is an old question I wanted to chime in. There are a few solutions for using rest in Unity. They all involve libraries that re-implement http over sockets. Here's one in particular: https://github.com/andyburke/UnityHTTP
It's too bad that the built in Unity http api doesn't support more verbs, and that the System.Net
classes aren't functional in webplayer.
I know the UnityHTTP library you posted isn't yours, but would you know if it needs to be compiled or if it can be used as is in Unity?
It seems to be looking for the UnityEngine and $$anonymous$$onoBehavior namespace.
Sorry if that's a stupid question :) This is the first time I've had to use external libraries in Unity.
I'm considering using UnityHTTP for my project but I was wondering what the other solutions are that you mentioned. Is UnityHTTP just the best one available and that you would recommend?
The functionality would serve but we were looking for something that is a bit more developed.
Hey @Antarxes, I'm looking at this stuff and UnityHTTP too. How did you get on?
@G$$anonymous$$T Hey there, we considered using WA$$anonymous$$ (gonna have to search for it, I forgot who made it) but we eventually ended up deciding to spend the money on the $$anonymous$$atrix library for X$$anonymous$$PP. We can send receive our messages to Unity that way. Not much of a solution for Indie devs though. It's working rather well but development is still early and no heavy lifting is being done yet.
We've created our REST SD$$anonymous$$ on the shoulders of UnityHTTP. It provides API similar to restangular, implements path building, error handling, and other useful things. You can find it here: https://github.com/vedi/restifizer-unity3d
Answer by kmfjsc · Nov 30, 2016 at 09:23 AM
I've done for doing this below. Let's go : ==>
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class btnGetData : MonoBehaviour {
void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(TaskOnClick);
}
IEnumerator WaitForWWW(WWW www)
{
yield return www;
string txt = "";
if (string.IsNullOrEmpty(www.error))
txt = www.text; //text of success
else
txt = www.error; //error
GameObject.Find("Txtdemo").GetComponent<Text>().text = "++++++\n\n" + txt;
}
void TaskOnClick()
{
try
{
GameObject.Find("Txtdemo").GetComponent<Text>().text = "starting..";
string ourPostData = "{\"plan\":\"TESTA02\"";
Dictionary<string,string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
//byte[] b = System.Text.Encoding.UTF8.GetBytes();
byte[] pData = System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
///POST by IIS hosting...
WWW api = new WWW("http://192.168.1.120/si_aoi/api/total", pData, headers);
///GET by IIS hosting...
///WWW api = new WWW("http://192.168.1.120/si_aoi/api/total?dynamix={\"plan\":\"TESTA02\"");
StartCoroutine(WaitForWWW(api));
}
catch (UnityException ex) { Debug.Log(ex.Message); }
}
}
Answer by qJake · Mar 25, 2010 at 10:06 PM
The HTTP protocol only supports the GET and POST operations. REST is not part of HTTP, they're two completely different things. REST is just a specification for how the server acts and sends the data. If the data is over HTTP, you can get the REST data just like you would get the contents of a webpage (using GET). HTTP POST is only for posting forms or files back to the webserver. Any time you want to get anything off of a webserver, you use GET.
http://en.wikipedia.org/wiki/REST
http://en.wikipedia.org/wiki/HTTP
Reading both of those articles would probably be to your benefit.
Good answer. I would just add a $$anonymous$$or correction that POST requests aren't solely for just sending information to the server. All POST requests return a response, much like the return value of a function, so you can retrieve information from the server via a POST request too.
From the linked wikipedia article: "...HTTP defines eight methods (sometimes referred to as "verbs")...".
I am aware that rest is not a part of http, but it is an architecture often implemented over http. The idea (when implementing over http at least) is to map resources to urls and use the verbs defined by http to define operations on those resources.
I'm not trying to argument that this is the best way to design web apis, but I still would like to use the other 6 http verbs so I can communicate with the existing web apis out there.
I'm sorry, but I completely disagree that GET and POST are the only useful verbs. HEAD is useful to see if there are changes to the document, $$anonymous$$ETE and PUT can be used to delete and insert documents. REST certainly is not a http verb, but an architecture describing a way to structure a web api, and one of the ideas is to use the http verbs to describe the action to be taken on a resource. Perhaps you should read the two wikipedia articles you linked to. All I want to do is to have a bit more control over my http requests by setting the http verb, but forget I asked...
"GET and POST are the only useful verbs" This is 100% Bullplop and FUD! Further, statements like "...and REST isn't even one of those verbs" show me you have NO concept of what HTTP or REST. Reading your own posted articles would do YOU benefit. I've never read a more wrong posting on this site.
This is total nonsense. HTTP does support more than just GET and POST. It's really unfortunate that the WWW class is as limited as it is. if you look at requests going out on the Chrome dev tools, you'll see just how simple an HTTP request is to build.
Your answer
Follow this Question
Related Questions
Cookie not correctly set in UnityWebRequest in 2018.3 1 Answer
Set Content-Length header for UnityWebRequest POST requests in 2017.3? 1 Answer
How would I go about getting the results of a javascript variable to a string inside script? 3 Answers
How to access StartCoroutine in a static way 3 Answers
Once Unity loads (Web player) how do I prevent it from being *Focused*? 0 Answers