Exemplo n.º 1
0
        /// <summary>
        /// Send Request to server with jsonbody
        /// </summary>
        /// <param name="url">API link</param>
        /// <param name="bodyJson">json body</param>
        /// <returns></returns>
        public IEnumerator doPost(string url, string bodyJson, Action <APIReponse> callBack)
        {
            using (var request = new UnityWebRequest(url, "POST"))
            {
                byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJson);
                request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw);
                request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
                request.SetRequestHeader("Content-Type", "application/json");

                request.SendWebRequest();
                while (!request.isDone)
                {
                    yield return(null);
                }
                if (request.responseCode == 200)
                {
                    string reponseJson = request.downloadHandler.text;
                    callBack(APIReponse.textToReponse(reponseJson));
                }
                else
                {
                    Debug.Log(API.ERROR_CONNECT);
                    callBack(null);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// send request to server with api link
        /// </summary>
        /// <param name="url">api url</param>
        /// <returns></returns>
        public IEnumerator doGet(string url, Action <APIReponse> callBack)
        {
            using (var request = UnityWebRequest.Get(url))
            {
                Debug.Log(url);
                request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
                request.SendWebRequest();

                while (!request.isDone)
                {
                    yield return(null);
                }

                if (request.responseCode == 200)
                {
                    string reponseJson = request.downloadHandler.text;
                    callBack(APIReponse.textToReponse(reponseJson));
                }
                else
                {
                    Debug.Log(API.ERROR_CONNECT);
                }
            }
        }