Ruby 如何使用RESTAPI发布json数据
如何以JSON格式发送数据Ruby 如何使用RESTAPI发布json数据,ruby,json,rest,Ruby,Json,Rest,如何以JSON格式发送数据 require 'net/http' require 'uri' postData = Net::HTTP.post_form(URI.parse('http://localhost/restapi/index.php/api/posts'), {'id'=>9,'firstname'=>"test","lastname"=>"test"}) puts postData.body 我也试过了,
require 'net/http'
require 'uri'
postData = Net::HTTP.post_form(URI.parse('http://localhost/restapi/index.php/api/posts'),
{'id'=>9,'firstname'=>"test","lastname"=>"test"})
puts postData.body
我也试过了,但没有成功:
@toSend = {"id" =>5,"firstname" => "anurag","lastname" => "arya"}
例如:
@toSend.to_json
如果可能,避免在问题中使用粗体大写。一直大喊大叫是没有用的。那么,问题出在哪里呢?现在使用post_form解决了。ok@tadman,我会处理它。使用post_formNo以这种方式发送数据,这不会提交
jsonData
作为原始请求主体,而是提交一个带有单个键值对的URL编码请求data=
。
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
url = "http://localhost/restapi/index.php/api/posts"
uri = URI.parse(url)
data = {"id"=>11,
"firstname"=>"PWD","lastname"=>"last"}
headers = {'Content-Type' =>'application/json',
'Accept-Encoding'=> "gzip,deflate",
'Accept' => "application/json"}
http = Net::HTTP.new(uri.host,uri.port) # Creates a http object
#http.use_ssl = true # When using https
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.post(uri.path,data.to_json,headers)
puts response.code
puts response.body
postData=Net::HTTP.post_form(URI.parse('http://localhost/oecprashant/yiiIndex.php/api/rubyREST'),
{'data'=>jsonData})