2010-05-09 87 views
2

这是一个非常基本的问题,但我不完全清楚如何做到这一点。如何将POST/GET从导轨添加到带有Nestful的API?

我正在尝试使用具有基于Web的服务的第三方服务。该服务称为Postful。但我不清楚究竟该做什么?

我看过ActiveResource(http://api.rubyonrails.org/classes/ActiveResource/Base.html)和rest-client,但我仍然不清楚要创建的步骤,代码和文件。

我想使用Nestful,但我不完全清楚如何使这项工作。 http://github.com/maccman/nestful

http://www.postful.com/service/mail是服务之一(详细信息发现http://www.postful.com/developer/guide#rest),而是要上传图片我要上传以下(但我不知道我是如何真正做到这一点?)。谢谢!

> http://www.postful.com/service/upload 
> 
> Be sure to include the Content-Type 
> and Content-Length headers and the 
> image itself as the body of the 
> request. 
> 
> POST /upload HTTP/1.0 Content-Type: 
> application/octet-stream 
> Content-Length: 301456 
> 
> ... file content here ... 
> 
> If the upload is successful, you will 
> receive a response like the following: 
> 
> <?xml version="1.0" encoding="UTF-8"?> 
> <upload> 
> <id>290797321.waltershandy.2</id> 
> </upload> 
+0

你已经能够确定他们是否有上传文件RESTful接口? – nfm 2010-05-17 22:56:01

+0

我不这么认为 - 唯一的接口是上面描述的那个.... :( – Angela 2010-05-17 23:12:00

回答

1

Nestful真的很容易,你应该能够只是这样做:

Nestful.post 'http://www.postful.com/service/upload', :format => :multipart, :user => 'foo', :password => 'bar', :params => {:file => File.open('YOUR_FILE')} 
1

我看了一下他们的API,它似乎并不像附件上传是REST风格的 - 它看起来像只邮购创作是。所以,ActiveResource在这里不会做这个伎俩。

根据您的开发/生产环境,您可能需要考虑使用更通用的东西,如curl

从手册:卷曲是一种工具来传输数据到服务器或传输到服务器......该命令旨在无需用户交互工作。

你会想是这样的:

# Encode your username:password as base64 
USERNAME="[email protected]" 
PASSWORD="yourpostfulpassword" 
BASE64_ENCODED_AUTH = `echo $USERNAME:PASSWORD | base64` 

curl -F "@path/to/file/to/upload;type=application/octet-stream" http://[email protected]/service/upload 

我没有测试过这个,因为我没有一个用户名/密码 - 但它应该让你在正确的轨道上。

你可以把它放在/lib的脚本中,并使用你喜欢的方法从你的控制器调用它。

编辑:

所以我试图与一个虚拟的用户名和密码,并使用--verbose标志curl,和头看了看右边。我也收到了一个401 UNAUTHORISED的回应,所以它看起来很正常。

+0

你有没有使用Nestful,它看起来像一种与HTTP和REST API进行交互的方式...... – Angela 2010-05-17 23:49:05

+0

我还没有使用Nestful - 你给'curl'一个试试吗?在简单看看Nestful之后,好像你会调用'Nestful.post url,options = {}'(除非postful * *有RESTful附件上传)与使用'curl'几乎相当,只是你用Ruby而不是bash来写它。 – nfm 2010-05-18 09:17:46