发布原始字符串而不是表单数据?

时间:2014-07-22 12:06:37

标签: ruby rest http

你是怎么做到的:

response = Net::HTTP.post_form(uri, {"q" => "My query", "per_page" => "50"})

仅使用原始字符串而不是表单? 类似的东西:

response = Net::HTTP.normal_post(uri, "My dog likes to walk in the forest")

1 个答案:

答案 0 :(得分:4)

我一直在搜索并找到了这个答案(在另一个网站上)

url = URI.parse(“http://api.domain.com”)
begin
requestXml = “John john1234”
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url.path)
request.body = requestXml
request["Content-Type"] = “application/xml”
response = http.request(request)
response.code # => 200 Ok
response.body # => The body (HTML, XML, blob, whatever)
rescue
logger.debug “Error #{$!}”
end

信用:

http://www.blog.openshell.in/2011/03/nethttp-raw-post-ruby-code/

相关问题