如何编写 REST API? [英] How to write a REST API?

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

问题描述

我正在编写一个 iPhone 应用程序作为一个爱好项目,它需要一个网络服务来为其提供数据.这和我在工作中所做的并没有太大区别,但是在工作中我只编写视图和控制器.其他人负责编写模型,通常由客户提供网络服务.

I'm writing an iPhone app as a hobby project and it will need a web service to provide it with data. It's not very different from what I do at work, but at work I only write views and controllers. Someone else is responsible for writing the model and usually the clients provide the web service.

我以前做过一些网络编程,那时候每个人都在使用 MySQL 和 PHP,所以我的技能有点过时,但我有信心我能够使用我已经知道的技术来拉它.但是,我不想浪费时间使用过时的工具.我发现最先进的方法是编写 REST API.我在想应该有一些非常好的框架,只要你定义了一个模型,它们就会为你提供一个具有 CRUD 功能的 REST API.

I have done some web programming before, back when everyone were using MySQL and PHP, so my skills are a bit outdated, but I'm confident that I would be able to pull it of using the techniques I already know. However, I don't want to waste my time using obsolete tools. I've figured out that the state of the art would be to write a REST API. I was thinking that there should be some pretty good frameworks out there that pretty much just gives you a REST API with CRUD functionality as soon as you've defined a model.

我想我的问题是:启动和运行 REST API 的最快方法是什么?我真的只想专注于编写 iPhone 应用程序,而不是在这个 API 上花太多时间.如果我也能获得网络管理和修订历史,那就太好了.我还应该补充一点,API 不应该是公开的,因此对身份验证的支持也会很好.

I guess my question is: What would be the fastest way to get a REST API up and running? I really just want to focus on writing the iPhone app and not spend too much time on this API. It would be great if I could get web administration and revision history too. I should also add that the API isn't supposed to be public, so support for authentication would be great as well.

只是为了清楚.我不介意 PHP 框架.事实上,它可能会更好,因为我知道我目前的主机支持它.

Just to be clear. I wouldn't mind a PHP framework. In fact it could possibly be better since I know that my current hosting supports it.

推荐答案

只是想让你知道:

我最终使用了 Ruby on Rails.

I ended up using Ruby on Rails.

由于这个答案没有提供选择 Ruby on Rails 的原因,也没有说明如何用它编写 REST API,我想我会给你我的动力以及一些简单的说明.

Since this answer has been downvoted for not providing the reason behind choosing Ruby on Rails and also no instructions on how to write a REST API with it, I thought I would give you my motivation and some simple instructions.

我开始阅读一本关于 Ruby on Rails 的书,并意识到我需要做的就是使用脚手架,而且我免费获得了一个 JSON REST API.

I started reading a book about Ruby on Rails and realized that all I needed to do was to use scaffolding and I got a JSON REST API for free.

这是一个很好的入门指南:http://guides.rubyonrails.org/getting_started.html

Here's a good guide to get you started: http://guides.rubyonrails.org/getting_started.html

当您的 Ruby on Rails 环境启动并运行时,创建 REST API 并不比运行更难:

When you have your Ruby on Rails environment up and running, creating your REST API isn't harder than running:

$ rails generate scaffold Post name:string title:string content:text

(来自上面链接的示例.)我还发现 Rails 非常容易并且可以免费部署到 heroku,这意味着我不必为我非常基本的低流量 REST API 的托管付费.我很高兴使用 Ruby on Rails 的原因还有很多,但这超出了本问题的范围.

(Example from the above link.) I also found that Rails is very easy and free to deploy to heroku, which meant that I didn't have to pay for hosting for my very basic, low traffic, REST API. There are many other reasons why I am very happy to work with Ruby on Rails, but that's beyond the context of this question.

这篇关于如何编写 REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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