将参数传递给Web服务器的两种方法之间的区别? [英] Difference between two approaches to pass parameters to web server?

查看:126
本文介绍了将参数传递给Web服务器的两种方法之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的网络服务器,突然间,我的脑海里出现了这个问题。

有两种方法可以通过GET方法传递参数。首先是用户获取参数,比如 url /?para = var 左右。但是,只要我的后端解析器知道它,我也可以在URL中很难写这个参数,就像 url /< parameter> / 一样。这些方法之间有什么区别?哪些方法更好?

解决方案

路径参数



当参数标识特定的实体时,请使用路径参数。例如,考虑一个博客。 URL / blog / posts 会返回一个包含博客中所有可用帖子的集合。要找到标识符 sending-parameters-in-http 的博客文章,请使用以下内容:

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

如果没有发现任何博客文章的标识符发送参数-http ,a 404 应该返回状态码。



查询参数



使用查询参数来过滤资源集合。例如,假设您需要使用 java 标记过滤博文:

$ b

$ b

  GET / blog / posts?tag = java 

如果在 java 标记中找不到任何帖子,则 200 应该返回一个空数组状态码。



查询参数可用于分页:

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

当对一个集合的资源进行排序时,也可以使用它:

  GET / blog / posts?sort = title 



<要得到一系列以 title 排序的 java 标签的帖子,您可以使用以下内容:

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


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?

解决方案

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

这篇关于将参数传递给Web服务器的两种方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆