Ruby on rails Net::HTTP::Post.new请求在Ruby 2中返回空正文
在Ruby 2.0.0p195、Rails 4.0.0中,Net::HTTP::Post.new请求返回空的响应体Ruby on rails Net::HTTP::Post.new请求在Ruby 2中返回空正文,ruby-on-rails,ruby,Ruby On Rails,Ruby,在Ruby 2.0.0p195、Rails 4.0.0中,Net::HTTP::Post.new请求返回空的响应体 @toSend = { "zuppler_store_id" => 'X3r82l89', "user_id" => '1' }.to_json uri = URI("http://smoothpay.com/zuppler/gen_token_post.php") http = Net::HTTP.ne
@toSend = {
"zuppler_store_id" => 'X3r82l89',
"user_id" => '1'
}.to_json
uri = URI("http://smoothpay.com/zuppler/gen_token_post.php")
http = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new uri
req.content_type = "application/json"
req.body = @toSend # or "[ #{@toSend} ]" ?
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req)}
puts "Response #{res.code} - #{res.message}: #{res.body}"
此代码返回“响应200-确定:”
但它应该像这样返回:{“结果”:“成功”,“令牌”:“843E5BE88FB8CEE7D3244929177B4E”}
您可以通过键入以下url进行检查:
为什么res.body是空的?似乎服务不喜欢POST请求是
application/json
这项工作:
uri = URI("http://smoothpay.com/zuppler/gen_token_post.php")
http = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new uri
req.body = "zuppler_store_id=X3r82l89&user_id=1"
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req)}
res.body # => "{\"result\":\"success\",\"token\":\"9502e49d454ab7b7dd2699a26f742cda\"}"
换句话说,提供服务
应用程序/x-www-form-urlencoded
。特别是,它会将text/html
交还给您,您必须JSON.parse
。奇怪的服务。谢谢。回答得好!