Skip to content

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

@Awais7848

Description

@Awais7848

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;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions