Rails:JSON 仍然是 REST 吗? [英] Rails: Is JSON still REST?

查看:39
本文介绍了Rails:JSON 仍然是 REST 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Web 开发还很陌生,所以如果这个问题真的很愚蠢,请耐心等待.

I'm still very new to web development, so please bear with me if this question is really stupid.

我正在尝试遵循 REST 原则,但是,我非常欣赏 JSON 如何与 Ruby 以及通过 API 进行外部连接的任何事物一起流动.我知道在 REST 中将参数直接输入到 URL 中是非常标准的,如下所示:myurl.com/ExampleModel/MyIDParameter 并将 JSON(如果有)发送到该 URI,对吗?

I'm trying to follow a REST principles, however, I really appreciate how JSON flows with Ruby and anything that is connecting externally through an API. I know that its pretty standard in REST to input parameters directly into the URL like so: myurl.com/ExampleModel/MyIDParameter and send the JSON (if there is any) to that URI, right?

我想知道的是:从 URI 中删除参数(以及路由,如果我永远不会使用它)并将参数包含在 JSON 中是否违反 REST 原则?

What I would like to know is: Is it against REST principles to remove the parameter from the URI (and the routing if I will never use it) and include the parameter in the JSON?

例如,不是这样调用:

myurl.com/ExampleModel/id
{
    "name" : "My Name",
    "anotherParameter" : "A random string"
}

你会称之为:

myurl.com/ExampleModel
{
    "id" : 512,
    "name" : "My Name",
    "anotherParameter" : "A random string"
}

推荐答案

首先,免责声明:我不会用 Ruby 编程,所以我无法回答有关 Ruby 的具体问题.但我的回答是针对您的 REST 原则问题.

First, Disclaimer: I don't program in Ruby, so I cant answer specific questions on Ruby. But my answer is directed to your question of REST principles.

最后,答案是:这是您的应用程序如何运行的问题.我将首先解释开发人员使用的一种常用方法.我提到了常见,这不是黄金法则.

Finally, the answer: This is a matter of how your application plays out. I will first explain a common approach used by developers. I mentioned common and is not the golden rule.

当您指定 myurl.com/ExampleModel/id 时.这种模式的一个常见含义是用于 GET 或 PUT 操作.从某种意义上说,获取有关 ID 值 id 的某个对象的信息.PUT 从某种意义上说,我有一些请求正文附加了要更新到数据存储的请求,其对象的 ID 为 id.

When you specify myurl.com/ExampleModel/id. A common meaning of such a pattern is used in with GET or PUT operation. GET in the sense that I want information on some object of ID value id. PUT in the sense that I have some request body attached with the request to be Updated to the Data Store whose Object have a ID of id.

当您提到 myurl.com/ExampleModel 时的第二个 URL,一个常见的解释是向我显示 ExampleModel 集合中的所有记录.这几乎是编写 API 的开发人员使用的默认行为.

Your second URL when you mentioned just myurl.com/ExampleModel, A common interpretation is to show me all the records from the ExampleModel collection. This is pretty much the default behaviour used by developers who write APIs.

那么,当你想做事情的时候,它是否违反了 REST 原则,答案是否定的.但是,如果您将 API 交给其他人,他们可能会对此感到困惑.

So, does it break REST principles when you want to do things, the answer is NO. But if you hand your API to other people, the might get a bit confused over it.

所以你有它.现在由您来决定什么最适合您的应用.

So there you have it. Now it's upto you to decide what is best suited for your application.

这篇关于Rails:JSON 仍然是 REST 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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