kostasandre
kostasandre

Reputation: 83

C# Http Post receive json from body

I have this HttpPost method:

[HttpPost]    
public string Test([FromBody]List<Account> accounts)
{
   var json = JsonConvert.SerializeObject(accounts);
   Console.Write("success");
   return json;
}

and this is my Account class:

public class Account
{
    public int accountId;
    public string accountName;
    public string createdOn;
    public string registrationNumber;
}

This is my json file which i send with postman:

{
    "Account": [
    {
        "accountId": "1",
        "accountName": "A purple door",
        "createdOn": "25-07-2017",
        "registrationNumber": "purple"
    },
    {
        "accountId": "2",
        "accountName": "A red door",
        "createdOn": "26-07-2017",
        "registrationNumber": "red"
    },
    {
        "accountId": "3",
        "accountName": "A green door",
        "createdOn": "27-07-2017",
        "registrationNumber": "green"
    },
    {
        "accountId": "4",
        "accountName": "A yellow door",
        "createdOn": "25-07-2017",
        "registrationNumber": "yellow"
    }
    ]
}

If i send this json my method doesn't work and it returns a null object. The only way to make it work is by sending the object only without the "Account" like this:

[
    {
      "accountId": "1",
      "accountName": "A purple door",
      "createdOn": "25-07-2017",
      "registrationNumber": "purple"
    },
    {
      "accountId": "2",
      "accountName": "A red door",
      "createdOn": "26-07-2017",
      "registrationNumber": "red"
    },
    {
      "accountId": "3",
      "accountName": "A green door",
      "createdOn": "27-07-2017",
      "registrationNumber": "green"
    },
    {
      "accountId": "4",
      "accountName": "A yellow door",
      "createdOn": "25-07-2017",
      "registrationNumber": "yellow"
    }
]

But i want the previous file format. How could my method receive the previous JSON ?

Upvotes: 4

Views: 23379

Answers (2)

Dev Raj Gautam
Dev Raj Gautam

Reputation: 123

Add a wrapper for you class Account and change the method defination

public class Account
        {
            public int accountId;
            public string accountName;
            public string createdOn;
            public string registrationNumber;
        }
        public class AccountWrapper
        {
            public List<Account> Accounts { get; set; }
        }
public string Test([FromBody]AccountWrapper accounts)
    {

    }

Upvotes: 1

Power Star
Power Star

Reputation: 1894

Try to use this contract to achieve your requirement.

public class Rootobject
{
    public Account[] Account { get; set; }
}

public class Account
{
    public string accountId { get; set; }
    public string accountName { get; set; }
    public string createdOn { get; set; }
    public string registrationNumber { get; set; }
}

Method should be like this.

[HttpPost]    
public string Test([FromBody]Rootobject accounts)
{
   var json = JsonConvert.SerializeObject(accounts);
   Console.Write("success");
   return json;
}

Upvotes: 3

Related Questions