skip to Main Content

I have such a service

public Sell Sell(Guid guid, double Quantity)
{
    Sell sell = new Sell();
    var result = _connectionDB.States.Find(guid);
    sell.Name = result.Name;
    sell.EAN = result.EAN;
    sell.Profit = result.Profit;
    sell.Quantity = Quantity;
    sell.SellePriceBrutto = result.SellePriceBrutto;
    sell.GTU = result.GTU;
    sell.dateTimeSell = DateTime.Now;
    sell.PurchasePriceNetto = result.PurchasePriceNetto;
    sell.Id = Guid.NewGuid();
    _connectionDB.Sells.Add(sell);
    result.Quantity = result.Quantity - Quantity;
    _connectionDB.SaveChanges();
    return sell;
}

And controller

[HttpPut("Sell")]
public IActionResult Sell(Guid guid, double Quantity)
{
    var result = _connectionDB.States.Find(guid);
    if (result == null)
    {
        return Ok("No product in DB");
    }
    if (result.Quantity < 0)
    {
        return Ok("No Quantiti product in DB");
    }
    var sell = _userService.Sell(guid, Quantity);
    return Ok(sell);
}

This function does the following:

On the basis of Id, it searches for a product, changes its quantity and adds it to the sold table.
Generating link:
https://localhost:7038/api/Warhause/Sell?guid=76f42455-0885-4edb-9d0a-0ebdd7610e4f&Quantity=1

On the swagger side it works fine.
Is there any function from HttpClient that calls such an endpoint?

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for your answer, I didn't know that you can pass null in this function. This works for me:

        private async Task Send()
        {
            await HttpClient.PutAsync($"api/Warhause/Sell?guid={Id}&Quantity={Amount}", null);
            await BlazoredModal.CloseAsync();
        }
    

  2. Yes, you can use the HttpClient class to call the endpoint of your WCF service. Here’s an example of how you can make the PUT request using HttpClient:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    class Program
    {
        static async Task Main()
        {
            using (HttpClient client = new HttpClient())
            {
                Guid guid = new Guid("76f42455-0885-4edb-9d0a-0ebdd7610e4f");
                double quantity = 1;
    
                // Construct the URL with query parameters
                string url = $"https://localhost:7038/api/Warhause/Sell?guid={guid}&Quantity={quantity}";
    
                // Make the PUT request
                HttpResponseMessage response = await client.PutAsync(url, null);
    
                // Check the response status
                if (response.IsSuccessStatusCode)
                {
                    // Read the response content
                    Sell sell = await response.Content.ReadAsAsync<Sell>();
                    Console.WriteLine($"Sell successful: {sell.Name}");
                }
                else
                {
                    Console.WriteLine($"Sell failed: {response.StatusCode}");
                }
            }
        }
    }
    

    Make sure to replace the placeholder values with the actual guid and quantity you want to send in the request. The HttpClient class allows you to send HTTP requests and handle the response. In this example, we use PutAsync to make a PUT request to the specified URL.

    You can then process the response based on the status code returned. If the request is successful (status code 200-299), you can read the response content and deserialize it into the Sell object or any other relevant class.

    Remember to include the necessary using statements and adjust the code to fit your specific scenario.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search