问题
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