ASP Web API POST JSON

Prevouisly on Serversncode we created a WEB API that created Data into a Azure Table storage. This had a POST method that accepts the parameters, ASP WEB API.

But what if we wanted to pass the data in as part of a JSON. Well lets do that. Source code is here

First up lets create our Player Class, Last Time we created the PlayerEntity class this is for our table Storage but we don't want to pass that around, we want a player class so we can pass that into the POST method.

In Models create a new Class Player

public class Player
{
    public string Sport_VC { get; set; }
    public string Club_VC { get; set; }
    public string First_Name_VC { get; set; }
    public string Last_Name_VC { get; set; }
    public string Position_VC { get; set; }
}

To make a clear break from this work and the last post we're going to create a new WebAPI Controller called PlayerJController.

    [HttpPost]
    public string Post([FromBody] Player record)
    {
        string sResponse = "";

        // Create the entity with a partition key for sport and a row
        // Row should be unique within that partition
        PlayerEntity _record = new PlayerEntity(record.Sport_VC, sRow);

        _record.Sport_VC = record.Sport_VC;
        _record.First_Name_VC = record.First_Name_VC;
        _record.Last_Name_VC = record.Last_Name_VC;
        _record.Club_VC = record.Club_VC;
        _record.Position_VC = record.Position_VC;

        return sResponse;
    }

I've cleaned the code above of the work we do to create the Azure table etc. Just to keep it simple.

.NET will handle the heavy lifting for us. If we open Postman now and create a new POST call to our new API method.

Test API

  • Change it to a Post Method
  • Select Body, and RAW type out the JSON body
  • Change the Application type to "JSON (application/json)
  • Enter the URL localhost:999090/API/PlayerJ

That's it when you run that it will send the JSON into the API method and you can work with it to create the player entity and store it in Azure Table storage.


CloudStatus

We have been using cloud services to build products and web sites getting alerts has helped us stay ahead of issues that might happen. We track over 300 sources of information for Microsoft Windows Azure, Google Cloud, Amazon Web Services, MailGun, Github, bitbucket and more.

Check it out
comments powered by Disqus