public IEnumerator PostJson <T>(string url, IRestJsonResponseHandler <T> handler, object payload) { using (UnityWebRequest req = new UnityWebRequest(url)) { req.method = UnityWebRequest.kHttpVerbPOST; req.SetRequestHeader("Content-Type", "application/json"); var uploader = new UploadHandlerRaw(Encoding.UTF8.GetBytes(ToJson(payload))); uploader.contentType = "application/json"; var downloader = new DownloadHandlerBuffer(); req.uploadHandler = uploader; req.downloadHandler = downloader; yield return(req.SendWebRequest()); if (req.isHttpError) { handler.OnHttpError(req.responseCode, req.error); } else if (req.isNetworkError) { handler.OnFailure(req.error); } else { handler.OnSuccess(FromJson <T>(req.downloadHandler.text)); } } }
public IEnumerator GetJson <T>(string url, IRestJsonResponseHandler <T> handler) { using (UnityWebRequest req = UnityWebRequest.Get(url)) { yield return(req.SendWebRequest()); if (req.isHttpError) { handler.OnHttpError(req.responseCode, req.error); } else if (req.isNetworkError) { handler.OnFailure(req.error); } else { handler.OnSuccess(FromJson <T>(req.downloadHandler.text)); } } }