كيف يمكنني استخدام REST-CLIENT لـ Ruby لنشر صورة؟

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

  •  30-09-2019
  •  | 
  •  

سؤال

أحاول إيجاد طريقة لاستخدام واجهة برمجة تطبيقات Postful وأحتاج إلى نشر صورة في جسم الطلب. أحاول استخدام العميل الراحة (لكنني منفتح على طرق أخرى):

الخطأ الذي أحصل عليه عندما أقوم بالنشر هو رمز حالة 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