开发者

How do I use rest-client for ruby to post an image?

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!&开发者_开发问答lt;/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


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜