Difference between two approaches to pass parameters to web server?

五迷三道 提交于 2019-12-18 07:12:42

问题


I am writing my web server and suddenly this question came into my mind.

There are two ways to pass parameters via a GET method. First is to user get parameters, such as url/?para=var or so. But I can also hard write this parameter in URL as long as my backend parser knows it, like url/<parameter>/. What's the difference between those methods and which is better?


回答1:


Path parameters

Use path parameters when the parameter identifies a specific entity.

For example, consider a blog. The URL /blog/posts returns a collection with all posts available in the blog. To find the blog post with the identifier sending-parameters-in-http, use the following:

GET /blog/posts/sending-parameters-in-http

If no blog post is found with the identifier sending-parameters-in-http, a 404 status code should be returned.

Query parameters

Use query parameters to filter a collection of resources.

For example, consider you need to filter the blog posts with the java tag:

GET /blog/posts?tag=java

If no posts are found with the java tag, a 200 status code with an empty array should be returned.

Query parameters can be used for paging:

GET /blog/posts?start=1&limit=10

The also can be used when sorting resources of a collection:

GET /blog/posts?sort=title

To get a set of posts with the java tag sorted by title, you would have something as following:

GET /blog/posts?start=1&limit=10&tag=java&sort=title



回答2:


As explained in When to use a routing rule vs query string parameters with asp.net mvc and many, many other resources on the web: it's all about aesthetics.

Query string parameters (&foo=bar) are considered "ugly", and URL routing (/foo/bar) is considered "clean". That's all.

It's not a functional difference, neither is "better". Search engines can handle both just fine, browsers handle both just fine, in fact, more and more browsers are moving towards hiding everything after the hostname in the URI anyway.

Often the SEO (Search Engine Optimization) argument is thrown in to defend routing, but that's just SEO sites pulling traffic to their blogs, parroting each other. It makes no difference at all when the values are the same.

It does make a difference when the values are different: the URI example.com/index.php?cat=4&subcat=2 will obviously score less than the URI example.com/products/furniture/seats.



来源:https://stackoverflow.com/questions/36418815/difference-between-two-approaches-to-pass-parameters-to-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!