Skip to content

Commit af93608

Browse files
committed
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).
1 parent 959a0cd commit af93608

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Helpers/HttpBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Proyecto26
88
{
99
public static class HttpBase
1010
{
11+
public static int HTTP_NO_CONTENT = 204;
12+
1113
public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action<RequestException, ResponseHelper> callback)
1214
{
1315

@@ -118,7 +120,7 @@ public static IEnumerator DefaultUnityWebRequest<TResponse>(RequestHelper option
118120
var body = default(TResponse);
119121
try
120122
{
121-
if (err == null && res.Data != null && options.ParseResponseBody)
123+
if (err == null && res.StatusCode != HTTP_NO_CONTENT && res.Data != null && options.ParseResponseBody)
122124
body = JsonUtility.FromJson<TResponse>(res.Text);
123125
}
124126
catch (Exception error)
@@ -139,7 +141,7 @@ public static IEnumerator DefaultUnityWebRequest<TResponse>(RequestHelper option
139141
var body = default(TResponse[]);
140142
try
141143
{
142-
if (err == null && res.Data != null && options.ParseResponseBody)
144+
if (err == null && res.StatusCode != HTTP_NO_CONTENT && res.Data != null && options.ParseResponseBody)
143145
body = JsonHelper.ArrayFromJson<TResponse>(res.Text);
144146
}
145147
catch (Exception error)

0 commit comments

Comments
 (0)