tags:

views:

275

answers:

2

Hi, 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) 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>
A: 

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="[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

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.

nfm
Have you used Nestful, it looks like a way to interace with HTTP and REST API....
Angela
I haven't used Nestful - did you give `curl` a try? After a brief look at Nestful it seems like you're gonna be calling `Nestful.post url, options = {}` (unless postful *does* have RESTful attachment uploading) which is going to be pretty much equivalent to using `curl`, except you're writing it in Ruby rather than bash.
nfm
A: 

I am still awaiting a good answer, but the bounty is going to expire and no one has answered it yet....

Angela