Reputation: 65
I have been tasked with building an API, that as a request, will take a product number which will have a quantity and size , zip code, shipping method.
The customer has a cart and in that cart is the product number, quantity and size so basically he would send a json request that looks like the below
{
"ShoppingCart": {
"Products": [
{
"Sku": "123",
"Size": "S",
"Quantity": "1"
},
{
"Sku": "456",
"Size": "M",
"Quantity": "2"
},
{
"Sku": "789",
"Size": "L",
"Quantity": "3"
}
],
"ShipToZip": "54452",
"ShipMethod": "Ground"
}
}
is it possible to receive an HTTP json request on my .net core rest webapi that im making.
If so, what would the route look like to send json like that? it'sgoing to be pretty long if they have to put the entire json in the url right?
EDIT: After doing more research, I find that I can receive a POST request with JSON in the body, from there i should be able to read that json, do some stuff with it, and then return json back right? Am i correct?
Upvotes: 1
Views: 1876
Reputation: 65
I guess I did not look hard enough :(
Anyway the more questions I feel like the more exposure some newbie problems will have.
How to receive json in ASP.NET web api?
I got my solution from there,
All I had to do was create a "Model" class like below that matches the exact JSON format being sent, no deserielization needed
public class RequestModel
{
public ShoppingCart RequestShoppingCart { get; set; }
}
public class ShoppingCart
{
public Products[] Products { get; set; }
public int ShipToZip { get; set; }
public string ShipMethod { get; set; }
}
public class Products
{
public string Sku { get; set; }
public string Size { get; set; }
public int Quantity { get; set; }
}
Then from there in my API Controller i can do the following to see it working
[Produces("application/json")]
[Route("api/ShippingCalculator")]
public class ShippingCalculatorController : Controller
{
// POST: api/ShippingCalculator
[HttpPost]
public string Post([FromBody]RequestModel jsonRequest)
{
// Debug.WriteLine(jsonRequest.RequestShoppingCart.Products);
return jsonRequest.RequestShoppingCart.ShipMethod;
}
}
Upvotes: 1
Reputation: 141472
After doing more research, I find that I can receive a POST request with JSON in the body, from there i should be able to read that json, do some stuff with it, and then return json back right? Am i correct?
Yes. You are correct. For instance, the following controller action would accept a POST body with the JSON from your question and respond with that same JSON.
public class Product
{
public string Sku { get; set; }
public string Size { get; set; }
public int Quantity { get; set; }
}
public class Cart
{
public List<Product> Product { get; set; }
public string ShipToZip { get; set; }
public string ShipMethod { get; set; }
}
public class CartBody
{
public Cart Cart { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// POST api/values
[HttpPost]
public ActionResult<CartBody> Post(CartBody cartBody)
{
return cartBody;
}
}
Upvotes: 1