0

I have one api method,i pass "/api/stocks/1/images/" and list of stockimage as json. I am getting stockImages below as null (function's input parameter)

[Route("api/stocks/{stockId}/images")]
public IHttpActionResult Post(int stockId, [FromBody]List<StockImage> stockImages)
    {
        return Ok();
    }

When i pass /api/stocks/1/images/ and list of stockimage as json and wrap it in a class and not accept list directly, I am able to see stockImages prefilled (function's input parameter)

I have one api method

[Route("api/stocks/{stockId}/images")]
    public IHttpActionResult Post(int stockId, [FromBody]Temp stockImages)
    {
        return Ok();
    }

public class Temp
{
    public List<StockImage> stockImages {get; set;}
}

Do i need to have this Temp wrapper class or is there any way to avoid this?

Adding sample json:

{  
   "stockImages":[  
            {  
               "imgId" : 8908,
               "imgURL": "http://imgd5.aeplcdn.com/cw/Volkswagen-Polo-Comfortline-4319619.jpg",
               "altText":"Honda City Exterior Photos",
               "title":"Honda City Exterior Photos",
               "defaultImg":true
            }
      ]   
}
2
  • 1
    can you show the json you are sending ? Commented Jul 8, 2016 at 7:35
  • To make your desired method signature work, how about just sending the array without the stockImages declaration? e.g. {[ { "imgId" : 8908, "imgURL": "http://imgd5.aeplcdn.com/cw/Volkswagen-Polo-Comfortline-4319619.jpg", "altText":"Honda City Exterior Photos", "title":"Honda City Exterior Photos", "defaultImg":true } ]} Commented Jul 8, 2016 at 7:55

1 Answer 1

1

So the problem is you are wrapping the array inside an object . remove it and you are good to go

[  
        {  
           "imgId" : 8908,
           "imgURL": "http://imgd5.aeplcdn.com/cw/Volkswagen-Polo-Comfortline-4319619.jpg",
           "altText":"Honda City Exterior Photos",
           "title":"Honda City Exterior Photos",
           "defaultImg":true
        }
  ]   
Sign up to request clarification or add additional context in comments.

2 Comments

Just one thing, which do you think is right approach- passing array within object or without wrapper object- convention wise?
@sahil , i dont see a need to wrap an array in another object unless you intend to pass some other metadata related to the array along with it.

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.