Tag:

如何通过curl发布数组值?

我喜欢测试API后端,其设计如下例所示: http://localhost:3000/api/v1/shops/1.json JSON响应: { id: 1, name: “Supermarket”, products: [ “fruit”, “eggs” ] } 这是相应的型号: # app/models/shop.rb class Shop < ActiveRecord::Base extend Enumerize attr_accessible :name, :products serialize :products, Array enumerize :products, in: %w{fruit meat eggs}, multiple: true resourcify validates :name, presence: true, length: { in: 5..50 } validates :products, presence: true end 我想使用curl来测试创建和更新条目。 因此,我使用以下命令: 创建: […]

Sinatra Net :: HTTP导致简单请求超时

我有一个简单的Net::HTTP POST请求要对我的Sinatra应用程序执行: def collect(website) uri = URI(“http://localhost:9393/save/#{website}”) res = Net::HTTP.post_form(uri, ‘q’ => ‘ruby’, ‘max’ => ’50’) puts res.body end 但它导致超时。 这是请求处理程序: post ‘/save/:website’ do |website| puts request.body “done” end 我从未达到过puts或done 。 我的shotgun服务器当然在端口9393上运行。 当我使用REST控制台扩展并在其中粘贴有效的json时,它适用于相同的路径。 导致此Timeout :: Error的原因是什么?

Ruby:如何使用Curb发送JSON POST请求?

如何将CURB请求的请求主体设置为我的json字符串? 我正在尝试使用Curb执行JSON POST请求。 我的代码: require ‘rubygems’ require ‘curb’ require ‘json’ myarray = {} myarray[‘key’] = ‘value’ json_string = myarray.to_json() c = Curl::Easy.http_post(“https://example.com” # how do I set json_string to be the request body? ) do |curl| curl.headers[‘Accept’] = ‘application/json’ curl.headers[‘Content-Type’] = ‘application/json’ curl.headers[‘Api-Version’] = ‘2.2’ end

如何使用Net :: HTTP :: Post将XML发布到RESTFUL Web服务?

我无法让这个工作,所以任何帮助将不胜感激! 基本上,request.body包含Web服务的有效XML,如下所示: Test Name 1 Some data for Unit testing …但该服务返回空XML。 请注意,返回id字段表明它实际上确实命中了数据库,但名称和描述字段为nil: 1 我已经使用Poster手动测试了RESTFUL服务,它运行正常。 这是代码: url = URI.parse(‘http://localhost:3000/someservice/’) request = Net::HTTP::Post.new(url.path) request.body = “Test Name 1Some data for Unit testing” response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)} #Note this test PASSES! assert_equal ‘201 Created’, response.get_fields(‘Status’)[0] 有没有人有任何线索为什么XMLpost中的数据不会持久存在?