Ruby:如何通过 HTTP 将文件作为 multipart/form-data 发布? [英] Ruby: How to post a file via HTTP as multipart/form-data?
本文介绍了Ruby:如何通过 HTTP 将文件作为 multipart/form-data 发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做一个 HTTP POST,它看起来像从浏览器发布的 HMTL 表单.具体来说,发布一些文本字段和一个文件字段.
I want to do an HTTP POST that looks like an HMTL form posted from a browser. Specifically, post some text fields and a file field.
发布文本字段很简单,net/http rdocs 中有一个示例,但我不知道如何发布文件.
Posting text fields is straightforward, there's an example right there in the net/http rdocs, but I can't figure out how to post a file along with it.
Net::HTTP 看起来不是最好的主意.curb 看起来不错.
Net::HTTP doesn't look like the best idea. curb is looking good.
推荐答案
我喜欢 RestClient.它用多部分表单数据等很酷的功能封装了 net/http:
I like RestClient. It encapsulates net/http with cool features like multipart form data:
require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))
它还支持流式传输.
gem install rest-client
会让你开始.
这篇关于Ruby:如何通过 HTTP 将文件作为 multipart/form-data 发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文