如何使用xml在Ruby中创建带有标题的Http帖子?
文档有类似这个curl命令的东西,我想将它转换为rails代码。
$headers = array("Content-type: text/xml", "Content-length: " . strlen($xml),
"Connection: close",);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
echo $data;if(curl_errno($ch))
print curl_error($ch);else
curl_close($ch);
我使用Net :: HTTP而且我有点卡住了,我试过了:
uri = URI("sample_url")
xml = 'sample xml'
req = Net::HTTP::Post.new(uri.path)
req['Content-Type'] = 'text/xml'
req['Content-length'] = xml.length
req['Connection'] = 'close'
req.body = xml
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
答案 0 :(得分:0)
试试这个:
uri = URI("sample_url")
xml = 'sample xml'
req = Net::HTTP::Post.new(uri, 'Content-Type' > 'text/xml', 'Content-length' => xml.length,'Connection' => 'close')
req.body = xml
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}