2012-06-11 52 views
0

我在使用ruby获取HTTP Put调用中传递的参数时遇到问题。看看“put_data”变量。 当我把它作为一个哈希,红宝石说:Ruby HTTP Put参数

undefined method `bytesize' for #<Hash:0x007fbf41a109e8> 

如果我转换为字符串,我得到: - “?令牌= wBsB16NSrfVDpZPoEpM”

can't convert Net::HTTPUnauthorized into String 

我也试着这样做只是

def process_activation 
     uri = URI("http://localhost:3000/api/v1/activation/" + self.member_card_num) 
     Net::HTTP.start(uri.host, uri.port) do |http| 
     headers = {'Content-Type' => 'text/plain; charset=utf-8'} 
     put_data = {:token => "wBsB16NSrfVDpZPoEpM"} 
     response = http.send_request('PUT', uri.request_uri, put_data, headers) 
     result = JSON.parse(response) 
     end 
     if result['card']['state']['state'] == "active" 
     return true 
     else 
     return false 
     end 
    end 

我搜索了所有的东西,包括rubydocs,但找不到如何编码参数的例子。任何帮助,将不胜感激。

回答

0

不要浪费你的时间与NET :: HTTP。我使用了'rest-client',并在几分钟内完成了这件事...

 def process_activation 
      response = RestClient.put 'http://localhost:3000/api/v1/card_activation/'+ self.member_card_num, :token => "wBsB1pjJNNfiK6NSrfVDpZPoEpM" 
      result = JSON.parse(response) 
      return result['card']['state']['state'] == "active" 
     end