Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

web services - Difference between two approaches to pass parameters to web server?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...