Ruby发送JSON请求 [英] Ruby send JSON request
本文介绍了Ruby发送JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在ruby中发送JSON请求?我有一个JSON对象,但我不认为我可以做.send
.我需要让javascript发送表单吗?
How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send
. Do I have to have javascript send the form?
或者我可以在ruby中使用net/http类吗?
Or can I use the net/http class in ruby?
带有标头-内容类型= json,主体为json对象吗?
With header - content type = json and body the json object?
推荐答案
uri = URI('https://myapp.com/api/v1/resource')
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = {param1: 'some value', param2: 'some other value'}.to_json
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
这篇关于Ruby发送JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文