开发者

How do I POST/GET from rails to API with Nestful?

开发者 https://www.devze.com 2022-12-29 10:28 出处:网络
this is a pretty basic question but I\'m not entirely clear how to do this. I am trying to use a third-party service that has web-based service.The service is called Postful.But I\'m not clear what e

this is a pretty basic question but I'm not entirely clear how to do this.

I am trying to use a third-party service that has web-based service. The service is called Postful. But I'm not clear what exactly to do?

I've looked at ActiveResource (http://api.rubyonrails.org/classes/ActiveResource/Base.html开发者_StackOverflow) and rest-client, but I'm still not clear exactly what steps, code, and files to create.

I'm trying to use Nestful but I'm not entirely clear how to make this work. http://github.com/maccman/nestful

http://www.postful.com/service/mail is one of the services (details found http://www.postful.com/developer/guide#rest ), but to upload an image I have to post the following (but I'm not sure how I actually do this?). Thanks!

> 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>


Nestful is really easy, you should be able to just do this:

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


I've had a look at their API and it doesn't seem like attachment uploading is RESTful - it looks like only mail order creation is. So, ActiveResource won't do the trick here.

Depending on your dev/production environment, you might want to look into using something more generic like curl.

From the manual: curl is a tool to transfer data from or to a server... The command is designed to work without user interaction.

You'll want something like this:

# Encode your username:password as base64
USERNAME="youremail@example.com"
PASSWORD="yourpostfulpassword"
BASE64_ENCODED_AUTH = `echo $USERNAME:PASSWORD | base64`

curl -F "@path/to/file/to/upload;type=application/octet-stream" http://$BASE64_ENCODED_AUTH@www.postful.com/service/upload

I haven't tested this as I don't have a username/password - but it should get you on the right track.

You can put this in a script in /lib and call it from your controller using your preferred method.

Edit:

So I tried this with a dummy username and password, and using the --verbose flag to curl, and the headers looked right. I also got a 401 UNAUTHORISED response so it looks like it's working right.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号