1

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)
}
3

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.