May 2018
Intermediate to advanced
334 pages
7h 25m
English
POST is used to create a resource. In our case, we will try to create a product using a POST request to the server. Before doing that, we will make a few changes to our project. You can find all related code on GitHub (https://github.com/PacktPublishing/Building-RESTful-Web-services-with-DOTNET-Core), so don't worry at all!
Why wait then? Let's write the Post method as follows:
// POST: api/Products[HttpPost]public async Task<IActionResult> Post([FromBody]Product product) => (await _productService.CreateProductAsync(product)) ? (IActionResult)Created($"api/products/{product.Id}", product) // HTTP 201 : StatusCode(500); // HTTP 500
The action method calls CreateProductAsync of the related service and checks if the operation was successful. ...