一个安静的 api 只使用干净的 url - 没有 url 变量或 post 变量 [英] a restful api only uses clean urls - no url variables or post variables

查看:35
本文介绍了一个安静的 api 只使用干净的 url - 没有 url 变量或 post 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

restful api 必须使用 get、post、put 或 delete 请求方法.提交的行为和数据完全由 uri 字符串决定.没有查询参数或发布变量.

A restful api has to use either get, post, put or delete request methods. The behavaiour and data submitted is entirely determined by the uri string. No query paramters or post variables.

这是真的吗?

有效:http://example.com/foo/84

无效:http://example.com/foo/?value=84

有效:

$.ajax({
  type: 'POST',
  url: "http://example.com/foo/84",
  success: success,
  dataType: dataType
});

无效:

$.ajax({
  type: 'POST',
  url: "http://example.com/foo/",
  data: 84,
  success: success,
  dataType: dataType
});

编辑到目前为止有两个答案,并且相互矛盾.

edit Two answers so far, and the contradict each other.

推荐答案

http://example.com/foo/?value=84 无效并不完全正确.我的意思是,只要它是一个有效的 URL,它就可以工作,您将能够通过 get 或 post 获取参数.

Saying that http://example.com/foo/?value=84 is not valid is not entireley true. What i mean is that as long that it is a valid URL it will work and you'll be able to get the parameters via get or post.

另一方面,REST 是一种架构,它的一个要求是一个干净的 URL(不包含带有?"的参数),因此这样的 URL 不被视为类似于 REST 的 URL.

On the other hand, REST is an architecture and one of its demands is a clean URL (one that does not include params with a '?') so such a url is not considered a REST like URL.

因此,如果您打算构建基于 REST 的应用程序,您应该只使用干净的 url.

So if you intend to build a REST based application, you should only use clean urls.

我从下面的评论中看到您在理解什么是 REST 时遇到问题,所以我将尝试举一个简单的例子:

I see from the comments below you have a problem understanding what is REST so i'll try to give a simple example:

  1. 为了获取数据,您可能会使用 http://example.com/foo/84 作为获取请求,其余的 FW 知道获取 id 为 84 的资源 foo.
  2. 为了发布关于 foo 的数据,你可以调用:http://example.com/foo/84 作为一个 POST 请求,现在 Rest FW 知道,因为它是一个 post 请求它将调用负责处理 post 的方法而不是处理 get 的方法
  3. 要删除,您使用 DELETE 操作调用相同的 URL,我想您知道其余的.
  1. In order to get data you will probably use http://example.com/foo/84 as a get request and the rest FW knows to get resource foo with id 84.
  2. In order to post data about foo, you might call: http://example.com/foo/84 as a POST request and now the Rest FW know that since its a post request it will call the method responsible for handling post and not the one for handling get
  3. To delete, you call the same URL with DELETE action and i think you know the rest.

因此,尽管您具有相同的 URL,但它是否是 GET/POST/PUT/DELETE 请求确实很重要.

So, although you have the same URL it really matters if its a GET/POST/PUT/DELETE request.

这篇关于一个安静的 api 只使用干净的 url - 没有 url 变量或 post 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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