我在使用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
我四处搜索,包括rubydocs,但找不到如何编码参数的示例。任何帮助将不胜感激。
答案 0 :(得分: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