我在字符串变量中有一个json表示,我想使用ruby将其发布到url。
到目前为止我有这个:
require 'json'
require 'net/http'
uri = URI.parse("http://www.example.com/post/here")
我的json字符串如下:
{"id":-1,"userId":1,"name":"some-test","createdAt":1356665463287}
如何获取该字符串并将其加载到ruby对象中然后发布?
答案 0 :(得分:10)
网络:在我看来,HTTP文档不值得付出努力。你可能会发现Faraday更好一些:
require 'faraday'
conn = Faraday.new(:url => 'http://www.example.com')
# post payload as JSON instead of "www-form-urlencoded" encoding:
conn.post do |req|
req.url '/post/here'
req.headers['Content-Type'] = 'application/json'
req.body = '{"id":-1,"userId":1,"name":"some-test","createdAt":1356665463287}'
end