可以在REST API中将查询参数设为必需吗?

huangapple go评论65阅读模式
英文:

can query parameters be mandatory in REST API

问题

我有一个需求,要求GET URI中必须有两个参数(其中一个表示日期),这两个参数必须是强制性的。但是这两个参数仅用于筛选的目的。所以我的问题是,强制性参数可以用作查询参数吗?(或者尽管没有父/子关系,我们仍然可以将它们用作路径参数)

英文:

I have a requirement which says there has to be two parameters(one of them represents date) that are part of the GET URI have to be mandatory. But these two parameters are only for filtration purpose. So my question is,
Can mandatory parameters be used as Query params? (Or we can still use them as path params although there is no parent/child relationship)

答案1

得分: 1

是的,强制参数可以用于查询参数。在这种情况下,您需要在调用API后进行验证,以检查参数的值是否不为null并且是否符合指定的格式。如果他们甚至在路径中未提及查询参数变量,那么他们最终会收到404错误。

英文:

Yes, mandatory parameters can be used in query parameters. In that case you need to put a validation after the API is hit to check whether the value of the parameter is not null and is of specified format. In case they fail to mention even the query parameter variable in the path, then they will end up receiving 404 error.

huangapple
  • 本文由 发表于 2020年4月4日 14:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定