Ruby:如何通过HTTP将文件作为multipart / form-data发布? [英] Ruby: How to post a file via HTTP as multipart/form-data?

查看:484
本文介绍了Ruby:如何通过HTTP将文件作为multipart / form-data发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个看起来像是从浏览器发布的HMTL表单的HTTP POST。具体来说,发布一些文本字段和一个文件字段。

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 。它将ne​​t / 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆