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

问题描述

我正在编写我的网络服务器,突然这个问题出现在我的脑海中.

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

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

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?

推荐答案

路径参数

在参数标识特定实体时使用路径参数.

例如,考虑一个博客.URL /blog/posts 返回一个包含博客中所有可用帖子的集合.要查找带有标识符 sending-parameters-in-http 的博客文章,请使用以下命令:

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

如果没有找到带有标识符 sending-parameters-in-http 的博客文章,一个 404 应该返回状态码.

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

使用查询参数来过滤资源集合.

例如,假设您需要使用 java 标签过滤博客文章:

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

GET /blog/posts?tag=java

如果没有找到带有 java 标签的帖子,一个 200 应该返回一个空数组的状态码.

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

查询参数可用于分页:

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

也可以在对集合的资源进行排序时使用:

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

GET /blog/posts?sort=title

要获取一组按 title 排序的带有 java 标签的帖子,您将有以下内容:

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 服务器的两种方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:24