1

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?

tjb
  • 65
  • 7
  • 1
    You only need to have a model (a C# class) that matches the JSON being sent. MVC will automatically bind the body of the request to an action method parameter. e.g. `public IActionResult PostMethod([FromBody]ShoppingCart cart) { ... }` – Brad Oct 12 '18 at 03:51
  • since the json request is coming in as a string, I need to deserialize it first right? then do what I need to with the important bits and put together a json response? am i over simplifying this? – tjb Oct 12 '18 at 03:59
  • MVC does all of this for you. This is basic functionality and all very well documented https://learn.microsoft.com/en-au/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1 – Brad Oct 13 '18 at 05:15

2 Answers2

1

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;
    }
}
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • So quick question, i see that you return an ActionResult. Say I wanted to take the JSON data sent to me and then do stuff with it, and make it into a JSON response. Is it bad practice to return a string of JSON? – tjb Oct 12 '18 at 04:53
  • 1
    That's a good question. I had to look that up too. Here is what I found: https://stackoverflow.com/questions/18482293/asp-net-mvc-contentresult-vs-string I'm afraid I don't have a more nuanced answer off the top of my head. – Shaun Luttin Oct 12 '18 at 04:56
1

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;
    }

}
tjb
  • 65
  • 7