0

I am designing a REST api for creating a resource using POST method. This create call accepts 4 parameters which are mandatory but not logically related to each other. So I have two options to accept these 4 input parameters as -

  1. Part of request as json object
    OR
  2. In the form of query parameters as (POST /api/someresource?param1=value1&param2=value2)

which option is most suitable?

Is there any guideline which suggests to choose one among above two methods based on the fact -

  1. that these are mandatory parameters so we should not use query parameters?
  2. these are not logically related but just a input to create a resource; so we can use query parameters?
Bruso
  • 803
  • 3
  • 12
  • 25

1 Answers1

0

/api/someresource?param1=value1&param2=value2 is likely a GET request and not POST request. If your request changes a state on the server then use POST. If its only a read operation use GET.

  • are you suggesting to follow convention or there is more to it? – Bruso Jul 24 '14 at 10:30
  • Short answer, both :) It makes the API more clear and understandable but additionally there may be cases where the same endpoint can link to two or more methods) /api/example can react differently to POST, GET, PUT, DELETE ... so to delete an item you don't need another endpoint like /api/deleteItem. You can use /api/item and remove the item when a DELETE request is made :) – user3811473 Jul 24 '14 at 10:35
  • yes I do agree conventional approach. Since these four input parameters are not logically related, I don't want to combine them under one input object and accept it at server side. – Bruso Jul 24 '14 at 10:45
  • I don't know all the details of your API but generally if you're doing something with entities within your method then you should have a relation between them (if one is needed) in the db. Then (/api/item example again) a GET request to list all items of a specific user would be achieved by joining the user and item tables. Therefore you avoid having to send so many "unrelated" fields with your request. Hope this help :) – user3811473 Jul 24 '14 at 12:00