0

我在使用 ruby​​ 获取 HTTP Put 调用中传递的参数时遇到问题。看一下“put_data”变量。当我将其保留为哈希时,ruby 说:

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

如果我转换为字符串,我会得到:

can't convert Net::HTTPUnauthorized into String

我也试过做 - '?token=wBsB16NSrfVDpZPoEpM'

   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

我到处搜索,包括 ruby​​docs,但找不到如何编码参数的示例。任何帮助,将不胜感激。

4

1 回答 1

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
于 2012-06-11T22:20:43.133 回答