Ruby 发送 JSON 请求 [英] Ruby send JSON request

查看:26
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆