0

I am trying to send an empty body to a Post Request but it does not execute.

I have already tried this:

Post an empty body to REST API via HttpClient

    static async Task<string> CancelSale(string mainUrl, string bearerInfo,string systemNumber)
    {
        var cancelsaleUrl = mainUrl + $"api/sale/cancel/{systemNumber}";
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerInfo);
        var data = new StringContent(null, Encoding.UTF8, "application/json");            
        var saleResponse = await client.PostAsync(cancelsaleUrl, data);
        var responseBody = await saleResponse.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);

        return responseBody;
    }

But it just does not execute, no exception.

I also tried this :

        var saleResponse = await client.PostAsync(cancelsaleUrl, null);

which also does the same result.

Any ideas?

LPLN
  • 475
  • 1
  • 6
  • 20
grozdeto
  • 1,201
  • 1
  • 13
  • 34
  • So when you debug through that it goes successfully through every line but the POST is never done? – Sami Kuhmonen Dec 11 '19 at 08:40
  • At the moment it reaches data and leaves the method. If I use the second option it leaves the method after saleResponse. And yes it does not do the POST – grozdeto Dec 11 '19 at 08:54

1 Answers1

0

The problem was very simple. I had the result of whole this method in a variable and It did not await the method:

        var cancelSale = CancelSale(mainUrl, bearerInfo, systemNumber);

Once it reaches anything that awaits it stops and leaves the method.

Here is the working code:

        var cancelSale = await CancelSale(mainUrl, bearerInfo, systemNumber);

        static async Task<string> CancelSale(string mainUrl, string bearerInfo,string systemNumber)
        {
        var cancelsaleUrl = mainUrl + $"api/sale/cancel/{systemNumber}";
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerInfo);
        var saleResponse = await client.PostAsync(cancelsaleUrl, null);
        var responseBody = await saleResponse.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
        return responseBody;
        }
grozdeto
  • 1,201
  • 1
  • 13
  • 34