Reputation: 33
How can I make a Http post with a header in Ruby with a xml?
The documentation have something like this curl command and I want to convert it to rails code.
$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);
I use Net::HTTP and I'm kinda stuck, I have tried:
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)
}
Upvotes: 1
Views: 312
Reputation: 752
Try this:
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)
}
Upvotes: 1