我正在编写发送 http post 请求的代码。现在我在我的代码中编写了 xml body,并且它可以正常工作。
但是如果我想使用 xml 文件发送请求,我会得到
未定义的方法 `bytesize' # 你的意思吗?字节
我的代码如下
require 'net/http'
request_body = <<EOF
<xml_expamle>
EOF
uri = URI.parse('http://example')
post = Net::HTTP::Post.new(uri.path, 'content-type' => 'text/xml; charset=UTF-8')
post.basic_auth 'user','passcode'
Net::HTTP.new(uri.host, uri.port).start {|http|
http.request(post, request_body) {|response|
puts response.body
}
}
**But if I want to make send file**
require 'net/http'
request_body = File.open('example/file.xml')
uri = URI.parse('http://example')
post = Net::HTTP::Post.new(uri.path, 'content-type' => 'application/xml; charset=UTF-8')
post.basic_auth 'user','passcode'
Net::HTTP.new(uri.host, uri.port).start {|http|
http.request(post, request_body) {|response|
puts response.body
}
}
我得到未定义的方法 `bytesize' # 你是说吗?字节
如果要将文件内容用作请求正文,则需要将文件内容加载到内存中,使用#read
方法:
request_body = File.open('example/file.xml').read
它会起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句