2

I have a web-api controller that accepts a single Json object Post (currently using Postman as my user-agent).

Here is my Post method:

    private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();    
    [ResponseType(typeof(Sales))]
            public HttpResponseMessage PostSales(Sales Sales, [FromUri] string auth)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        if (auth == "KDI")
                        {
                            Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
                            if (1 == rs)
                            {
                                return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
                            }
                            else
                            {
                                db.Sales_.Add(Sales);
                                db.SaveChanges();
                                return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
                            }
                        }
                        else
                        {
                            return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
                        }
                    }
                    else
                    {


  return Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong with the JSON model you sent me.");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

        }

And here is the Json:

{
  "ExtSerial": "AH0000002",
  "Date": "2015-03-01",
  "CustomerRefNbr": "JPM0001",
  "Description": "2015 FEBRUARY RENTAL  2015 FEBRUARY RENTAL",
  "Customer": "TRDE0065",
  "Amount": 17989.51,
  "AQ_Branch": "KDI",
  "AQ_COA": "4100503000",
  "LineSubAccount": "OPMOPN000",
  "LineTaxCategory": "JPMTAX",
  "LineQuantity": 1,
  "LineUnitPrice": 400000,
  "AQ_PostStatus": 1,
  "AQ_StatusDate": "2015-03-01",
  "DTS": "2015-03-01"
}

It works fine and all but I wanted to have my post method to optionally accept multiple array of Json data

Something like this:

[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]

I tried putting my sales model parameter as a list<> but it could not read my Sales entity db.Sales_.Add(Sales); and it could not get the table serial in my Sales entity in Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();

I am using EF6 and LINQ btw. I really need your help, I know it is a simple problem but I'm really stuck.

2 Answers 2

4

You cannot receive multiple JSON objects in a Web API POST action. That's because it can only deserialize the POST payload once.

The only solution is to post (client side) an receive (web action parameter) an object which has the other objects as properties.

Your question can not be very well understodd, however, I'll try to give you a more concrete answer. You need to define a class like this as your web api parameter:

public class SalesAndIds
{
    public Sales Sales { get; set; }
    public List<Ids> IdsList { get; set; }
    public class Ids
    {
        public int Id1 { get; set; }
        public int Id2 { get; set; }
        public int Id3 { get; set; }
    }
}

And the object that you post from JavaScript must look like this:

{
   Sales: { /* the sales object*/ },
   IdsList: [ /* the array with the ids list */  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

No, in Postman you can only compose requests. To test this, you need to create a POST request (change the method to POST on the drop down list) and then write by hand the JSON payload. To do so, choose "Raw" and then choose the "JSON" type to get editor help to write the JSON payload. Don't forget to add the Accept and Content-Type headers, both with value: application/json. The payload should look like the second code block in my answer, but with the values instead of teh comments
Sorry for the late reply. Is this considered a mulitple objects? {"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554} and can't I post that to my web api?
0

You can try something like below, receiving the post data in JArray instead of string

public <return type> <method name>(string id, [FromBody] JArray value)
  {
      if (value != null)
      {
         string data = value.ToString();
      }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.