设为首页 加入收藏

TOP

ruby相关:Linux下的回车
2014-11-23 23:26:54 来源: 作者: 【 】 浏览:12
Tags:ruby 相关 Linux 回车

今天,在Linux下用ruby写一段执行文件上传的post代码的时候,我写下如下代码:


require 'net/http'
require 'uri'



url = URI.parse('http://myserver/services/upload.xml')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'username', 'password'
req['content-type'] = 'multipart/form-data;boundary=bbb'
......
req.body = %Q{--bbb
Content-Disposition: form-data; name="upload_file[]"; filename="my_file"
Content-Type: application/octet-stream
this is the content of the upload file.
this is the content of the upload file.
this is the content of the upload file.
--bbb
Content-Disposition: form-data; name="upload_file[]"; filename=""
Content-Type: application/octet-stream
--bbb--}


res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
p res



不过这段代码始终执行错误,rails提示bad content之类。但是,我将header,body与w3g上的http关于multiple form的协议进行了比较,但是没发现有哪里不对。折腾了半天,只能调试跟踪到rails内部,发现rails在执行read_multipart方法,按照boundary截取request体的时候老是多截取一个字符。比如,对于body来说,我这里body的开始是--bbb,然后换行。但是在rails内部总是截取出--bbb C,多截取了一个C。纳闷了半天,突然想起,unix/Linux下面的换行是:换行符(LF),不像windows下面换行是:回车(CR)换行(LF),但是http协议正好是按照boundary+CRLF进行request的解析,所以,造成我的request始终解析错误。所以,我将上面代码,关于body的赋值改为如下:


req.body = %Q{--bbb\015
Content-Disposition: form-data; name="upload_file[]"; filename="my_file"
Content-Type: application/octet-stream\015\012\015
this is the content of the upload file.
this is the content of the upload file.
this is the content of the upload file.
--bbb\015
Content-Disposition: form-data; name="upload_file[]"; filename=""
Content-Type: application/octet-stream\015\012\015
--bbb--}



OK!大功告成,可以通过脚本成功的发送文件上传post请求。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux环境下多线程实现同时连接多.. 下一篇我的ARM-Linux初之旅

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: