문제

I'm designing a REST API that supports HTTP GET parameters. In most cases I only accept one value for a parameter. But how should I handle duplicate parameters?

For example, Stack Overflow accepts a GET param tab:

http://stackoverflow.com/?tab=hot
http://stackoverflow.com/?tab=featured

Duplicate parameters are allowed, passing both values is correct:

http://stackoverflow.com/?tab=hot&tab=featured

What should I do? Just go with the first value, thus silently ignoring other values (what SO does) or return an error stating only one value is allowed? In the latter case, what error should I return with what status code (409 Conflict, perhaps)?

도움이 되었습니까?

해결책

I agree with VKSingla that this is a design decision, therefore there is no correct answer only opinions in this matter.

If you ask me I would make a 'strict' API and just throw an error (I would make sure that it is a clear error and not just a random code which doesn't help the user). I prefer this strict approach because if usercode is adding the same param twice it will likely be a bug somewhere in the users code. Revealing this bug as early as possible helps the user finding the bug asap.

If you choose to go with ignoring the other parameters then make sure that the user knows this behavior. For example document 'all duplicate parameters after the first will be ignored'. Undocumented 'magic behavior' like this can make code pretty damn hard to debug.

다른 팁

This is a design decision. Its your API design on how you want it to function.
If you choose to ignore any one , then the question is which one ?
So, it is simply a conflict. or else
your API can respond with combined data, but the request for that should be like this

https://stackoverflow.com/?tab=hot,featured

Also refer this question Extra Query parameters in the REST API Url

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top