RestClient icon indicating copy to clipboard operation
RestClient copied to clipboard

[Issue] PATCH Request Does not work as Webrequest is not awaited properly

Open Awais7848 opened this issue 1 month ago • 0 comments

I Tried using patch request but it instantly falls back to Bad Request Error seems like it does not await the request properly this is an example working patch request i created. Can you guys please fix this.

      public static async Task Patch(
string urlPath,
string payload, Action<ResponseHelper, Exception> callback = null,
Dictionary<string, string> queryParams = null,
Dictionary<string, string> headers = null)
        {
            await Task.Delay(TimeSpan.FromSeconds(1f));

            string url = BuildUrlWithParams(urlPath, queryParams);
           

            UnityWebRequest request = UnityWebRequest.Put(url, payload);
            request.method = "PATCH"; // override PUT to PATCH

            request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(payload));
            request.downloadHandler = new DownloadHandlerBuffer();

            Dictionary<string, string> commonHeaders = new Dictionary<string, string>
                {
                 {
                    "Authorization", $"HMAC {GetBitString()} {CreateToken(payload)}" },

                    { "x-app-version", "1.10" },
                };

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    commonHeaders.Add(header.Key, header.Value);
                }
            }

            if (headers != null)
            {
                foreach (var h in headers)
                {
                    request.SetRequestHeader(h.Key, h.Value);
                }
            }

            // Await the request properly
            var asyncOp = request.SendWebRequest();
            await AwaitWebRequest(asyncOp);

            if (request.result != UnityWebRequest.Result.Success)
            {


               // callback.Invoke(new ResponseHelper(request));
                Debug.LogError($"PATCH failed: {request.responseCode} - {request.error}");
            }
            else
            {
                Debug.Log($"PATCH success: {request.downloadHandler.text}");
            }
        }

        // Helper to await UnityWebRequest properly in async method
        private static Task AwaitWebRequest(UnityWebRequestAsyncOperation operation)
        {
            var tcs = new TaskCompletionSource<bool>();
            operation.completed += _ => tcs.SetResult(true);
            return tcs.Task;
        }

Awais7848 avatar Nov 19 '25 11:11 Awais7848