【问题标题】:Convert curl command to Ruby http POST(xml) with headers将 curl 命令转换为带有标头的 Ruby http POST(xml)
【发布时间】:2018-07-28 20:26:33
【问题描述】:

如何在带有 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)
}

【问题讨论】:

标签: ruby-on-rails ruby http curl net-http


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2020-05-16
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2016-08-04
    • 2022-07-29
    • 2021-12-05
    相关资源
    最近更新 更多