0

So, I am trying to use the REST API to access my firebase database, but unitywebrequest is giving me some issues.

I have noticed that the code I have returns the html code of the page only for certain websites (I have tried it with www.google.com) but with other websites (like www.youtube.com or my firebase database website) it just returns a response code of 0 and an empty message.

Any idea on what could be causing this? I am a newbie when it comes to APIs and get/post requests.

My code:

public class DatabaseHandler : MonoBehaviour
{

    string url = "https://locked-3426c.firebaseio.com/levels.json";
    void Start()
    {
        StartCoroutine(GetLevelsCoroutine());
    }

    IEnumerator GetLevelsCoroutine()
    {    
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
            www.SetRequestHeader("X-Firebase-Decoding", "1");
            www.SetRequestHeader("Accept", "text/event-stream");
            www.downloadHandler = dH;
            yield return www.SendWebRequest();
            Debug.Log(www.responseCode);
            string result = www.downloadHandler.text;
            Debug.Log(result);
        }
    }

}
  • Which Unity Version are you using? There is a [known issue](https://issuetracker.unity3d.com/issues/unity-does-not-allow-to-use-tls-version-higher-than-1-dot-0) regarding TLS versions. Maybe that's your problem, too – derHugo Dec 04 '18 at 17:22
  • I am using Unity-2018.2.7f1 If that version is affected by that bug, did you find a workaround it or an alternative to unitywebrequest to access the REST API of Firebase Database? – Domenico Rotolo Dec 04 '18 at 17:28
  • It claims to be fixed in 2018.2. so you shouldn't be effected. Did you add credentials? You also have two `;` after the `SendWebRequest` – derHugo Dec 04 '18 at 17:30
  • Also are you sure you have to attach a `DownloadHandlerBuffer`? Look at [this example](https://docs.unity3d.com/Manual/UnityWebRequest-RetrievingTextBinaryData.html) – derHugo Dec 04 '18 at 17:35
  • Sorry for the double ";" I have edited the post. I didn't add credentials yet because I have defined the database as public for testing, but I don't think the problem is related to firebase because this code doesn't work for other websites too. – Domenico Rotolo Dec 04 '18 at 17:37
  • I have been tinkering with the code to try to make it work, I added the DownloadHandlerBuffer only afterward. Either way it still doesn't work – Domenico Rotolo Dec 04 '18 at 17:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184707/discussion-between-domenico-rotolo-and-derhugo). – Domenico Rotolo Dec 04 '18 at 17:40

0 Answers0