tags:

views:

195

answers:

1

I am trying to find a way to use the Postful API and I need to post an image in the body of the request. I am trying to use rest-client (but am open to other ways):

The error I get when I post is HTTP status code 422

Basically, I'm clueless and need guidance. :\

class PostfulsController < ApplicationController
  require 'rest_client'

  def submit

    body = "<?xml version='1.0' encoding='UTF-8'?>
               <mail>
                <documents>
                  <document>
                    <template>
                      <source>gallery</source>
                      <name>Postcard: Image fit front</name>
                    </template>
                    <sections>
                       <section>
                         <name>Text</name>
                         <text>Hello, World!</text>
                        </section>
                     <section>
                        <name>Image</name>
                        <attachment>#{attachment_id}</attachment>
                        </section>
                     </sections>
                  </document>
                </documents>
                <addressees>
                  <addressee>
                    <name>John Doe</name>
                    <address>123 Main St</address>
                    <city>Anytown</city>
                    <state>AZ</state>
                    <postal-code>10000</postal-code>
                  </addressee>
                </addressees>
              </mail>"

      result = RestClient.post 'http://www.postful.com/service/mail', 
               body, :content_type => 'text/plain'

     end 
end
A: 

Try

# result will contain the response
result = RestClient.post('http://www.postful.com/service/mail', 
                    request.body,
                    {:content_type => 'text/plain',
                     'Authorization: Basic' => 'QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
p result.body
p result.code
p result.headers
Archiloque
ah, thanks....I think I can start to play with this....
Angela
Hi, I get an error around the body= method...undefined method `body=' Is the body method coming from rest client? I use require 'rest_client" in the controller and installed the gem..
Angela