From c4d3841100b5de22e628dbad27ab360348b7a94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl?= Date: Thu, 7 Oct 2021 16:15:36 +0200 Subject: [PATCH] Fix: Handle HTTP NO CONTENT status code (204) to prevent null reference exceptions. NO_CONTENT is not considered as being an error, and therefore previous code would try to parse body data even with a NO_CONTENT being returned. Various backend frameworks use NO_CONTENT as a way to gracefully indicates that a specific resource is empty (for instance, if you try to get the list of items in a player's inventory and the inventory is empty). --- Proyecto26.RestClient/Helpers/HttpBase.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Proyecto26.RestClient/Helpers/HttpBase.cs b/Proyecto26.RestClient/Helpers/HttpBase.cs index 1e7a8e4..63939cb 100644 --- a/Proyecto26.RestClient/Helpers/HttpBase.cs +++ b/Proyecto26.RestClient/Helpers/HttpBase.cs @@ -8,6 +8,8 @@ namespace Proyecto26 { public static class HttpBase { + public static int HTTP_NO_CONTENT = 204; + public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action callback) { @@ -118,7 +120,7 @@ public static IEnumerator DefaultUnityWebRequest(RequestHelper option var body = default(TResponse); try { - if (err == null && res.Data != null && options.ParseResponseBody) + if (err == null && res.StatusCode != HTTP_NO_CONTENT && res.Data != null && options.ParseResponseBody) body = JsonUtility.FromJson(res.Text); } catch (Exception error) @@ -139,7 +141,7 @@ public static IEnumerator DefaultUnityWebRequest(RequestHelper option var body = default(TResponse[]); try { - if (err == null && res.Data != null && options.ParseResponseBody) + if (err == null && res.StatusCode != HTTP_NO_CONTENT && res.Data != null && options.ParseResponseBody) body = JsonHelper.ArrayFromJson(res.Text); } catch (Exception error)