2

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?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
alvinzoo
  • 493
  • 2
  • 8
  • 17

2 Answers2

3

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
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272