1

My question is little theoretical but it's interesting one. I want to know which approach is best while using web api.

I've two methods

  1. One has List<Customer> as parameter

public void Create(List<Customer>) { //... }

  1. Second has CustomerList as parameter (Where CustomerList class has property public List<Customer> customers {get; set;})

public void Create(CustomerList customer) { //... }

I know how for method 1, I need to pass json array and for method two I need to wrap json array to json object.

But my question is which approach is best and why?

1
  • 1
    There are no pros and cons. If you just want customers, you use List<Customer>. On the other hands, if you want customers and additional data, you use latter approach. Commented Dec 28, 2016 at 13:48

1 Answer 1

3

When it comes to APIs, I always try to be as explicit as possible when it comes to the contract it exposes. So when you expect a List, use a JSON array. It will be much clearer to users of your API what is expected.

Having said that, the advantage of using a JSON object is that it won't be a breaking change if you decide to accept extra properties with your request later on.

In the end, it all depends on your use-case. Is this going to be an endpoint exposed to other people than just you? Do you already know you're going to want to accept additional data along with the customer list in the future? These are some of the questions you need to ask yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Quite descriptive answer. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.