2012-06-11 97 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