- Home /
The question is answered, right answer was accepted
I cant get response json from rest call in Unity
Hi all,
I had a problem while using www class for rest calls. I wrote the following code.
void Start ()
{
WWW www = new WWW ("http://ip.jsontest.com/");
StartCoroutine(WaitForRequest (www));
}
private IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null) {
print (www.text);
} else {
print ("error: "+www.error);
}
}
"http://ip.jsontest.com/" this url returns simple json that shows an ip.
Ex: { "ip": "193.202.18.14" }
But I didnt get that json in unity. What i get is below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IIS Windows Server</title>
<style type="text/css">
<!--
body {
color:#000000;
background-color:#0072C6;
margin:0;
}
#container {
margin-left:auto;
margin-right:auto;
text-align:center;
}
a img {
border:none;
}
-->
</style>
</head>
<body>
<div id="container">
<img src="iis-85.png" alt="IIS" />
</div>
</body>
</html>
UnityEngine.MonoBehaviour:print(Object)
<WaitForRequest>c__Iterator0:MoveNext() (at Assets/ApplicationController.cs:56)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
I have read many documents and I have tried many solutions but I couldn't solve it. How could i get that json?
Thanks,
Answer by ScaniX · Aug 24, 2016 at 02:29 PM
At first I wanted to suggest the things below the line, but now I think you have some local problem. Did you try the same request using curl or any other program? I thought the headers might be the problem, but both curl and a request from within java HTTPUrlConnection return the expected contents.
I don't know what you have tried yet and cannot test it here, but you could try a different constructor to pass an "Accept: application/json" header value.
To keep it a GET request, check out this answer:
http://answers.unity3d.com/questions/63873/adding-header-info-to-www-get-method.html
Yes, you are right it is a local problem. I didn't realize it before because i could send request via rest client tools such as "postman". But I realized that Firewall or proxy settings blocking my request. Now it works like a charm. Thanks.