user1474111
user1474111

Reputation: 1516

Creating two different rest api with optional values

I am just new to rest api and need to create two api. both of them use post.

1) First one takes 2 parameters inside of the body

url is : /myapp/resources

2) Second one is doing the same function as the first one does but it needs 2 optional parameter. I thought using path or request param but as the parameters are optional it will be confusing when there is no paramaters. So there will be conflict with the first api.

/myapp/resources?param1=xx&param2=xx ==> what if there is no parameter
/myapp/resources/param1/{xxx}/param2/{yyy} ==> so still what if there is no param:

So what is the best way to define second api without causing a conflict with the first api when optional parameters are not passed.

Upvotes: 0

Views: 465

Answers (1)

Rozart
Rozart

Reputation: 1788

I would suggest going with the first approach (query params) so: /myapp/resources?param1=xx&param2=xx

The reason being - in this case you can define or extend the logic of just one endpoint, the /myapp/resources, that is going to verify if parameters exist and react accordingly.

With the second approach (path params), you would have to most likely define two (or more) separate endpoints (like /myapp/resources/, /myapp/resources/param1/{param1} and /myapp/resources/param1/{param1}/param2/{param2}

Upvotes: 1

Related Questions