Upload image post request over http to other application using Paperclip

我们两清 提交于 2020-01-06 17:58:41

问题


I am able to upload image attachements using paperclip to my application. But I would like to send the image to another application via faraday connection. Other attributes are successfully sent but not the attachment image. I am not sure what I should do to achieve this.


回答1:


You need to use an HTTP library that can create Multipart Post requests.

For example Typhoeus can do file uploads: https://github.com/typhoeus/typhoeus#handling-file-uploads.

There's also Net::HTTP Multipart Post: https://github.com/nicksieger/multipart-post

And finally: https://github.com/jwagener/httmultiparty

Read the READMEs to any of those gems, they all make it equally easy to do file uploads. I like Typhoeus because it can do parallel requests. The other two are a bit simpler but equally useful.

Learn more about what a multipart form post is:

  • What is http multipart request?
  • application/x-www-form-urlencoded or multipart/form-data?
  • http://www.huyng.com/posts/under-the-hood-an-http-request-with-multipartform-data/



回答2:


If I have to stick with using faraday, here is how to achieve the posting of paperclip attachments.

    Faraday.new(:url => url) do |faraday|
        faraday.request :multipart
    end

And put the attachement as such in where post request happens

params['avatar'] = Faraday::UploadIO.new(avatar.map.path, 'image/jpeg')


来源:https://stackoverflow.com/questions/29853278/upload-image-post-request-over-http-to-other-application-using-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!