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
Thank you for your answer, I didn't know that you can pass null in this function. This works for me:
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 usingHttpClient
:Make sure to replace the placeholder values with the actual
guid
andquantity
you want to send in the request. TheHttpClient
class allows you to send HTTP requests and handle the response. In this example, we usePutAsync
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.