Woocomerce : Update a product doesn't work

43 Views Asked by At

I'm trying to update a product's informations to the WooCommerce shop, from a windows service as follows:

 using (var client = new HttpClient())
 {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

               string encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));


                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
                var _url = $"{WooUrl}/wp-json/wc/v3/products/{id}";

                //var request = new HttpRequestMessage(HttpMethod.Post, _url);
                ////request.SetTimeout(TimeSpan.FromSeconds(10));                  
                                     
                var requestBody = JsonConvert.SerializeObject(p); 
                //request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");

                var response = await client.PostAsync(_url, new StringContent(requestBody, Encoding.UTF8, "application/json"));
                
            }  

But I'm getting trouble as the await doesn't finish even the update is done in the website, because i need the response data.

Assuming that the same request is working from Postman when it's simulated.

How to resolve this issue?

thanks.

1

There are 1 best solutions below

0
On

i figure out the issue finally. By calling .Result, i was blocking the main thread, which causes a deadlock and the postasync doesn't return.

I change my handler to async void and use await instead.