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

pep*_*dip 2 ruby rest http

你怎么做到这一点:

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

仅使用原始字符串而不是表单?就像是:

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

pep*_*dip 5

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

\n\n
url = URI.parse(\xe2\x80\x9chttp://api.domain.com\xe2\x80\x9d)\nbegin\nrequestXml = \xe2\x80\x9cJohn john1234\xe2\x80\x9d\nhttp = Net::HTTP.new(url.host, url.port)\nrequest = Net::HTTP::Post.new(url.path)\nrequest.body = requestXml\nrequest["Content-Type"] = \xe2\x80\x9capplication/xml\xe2\x80\x9d\nresponse = http.request(request)\nresponse.code # => 200 Ok\nresponse.body # => The body (HTML, XML, blob, whatever)\nrescue\nlogger.debug \xe2\x80\x9cError #{$!}\xe2\x80\x9d\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

学分至:

\n\n

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

\n