RubyにRest-clientを使用して画像を投稿するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2863164

  •  30-09-2019
  •  | 
  •  

質問

私はポストフルAPIを使用する方法を見つけようとしています。リクエストの本文に画像を投稿する必要があります。私は休憩クライアントを使用しようとしています(ただし、他の方法で開かれています):

投稿したときに得られるエラーはHTTPステータスコード422です

基本的に、私は無知であり、ガイダンスが必要です。 :

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
役に立ちましたか?

解決

試す

# 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top