Ruby中如何获取PUT请求返回的URL值?遇URL为null问题
问题:PUT请求返回URL为null,无法获取目标值
我需要获取PUT请求返回的URL值,但当前PUT请求返回结果为:
{"result":{"attempts":1,"url":null}}
无法定位问题所在。
操作流程
- 先向
/challenges发送POST请求启动挑战 - 在响应的
actives_at字段指定的时间,向/challenges发送PUT请求,请求头携带X-Challenge-Id(挑战ID)
实现代码
loop do response = HTTParty.put("#{API_ENDPOINT}/challenges", headers: { 'X-Challenge-Id' => challenge_id }) puts "Response: #{response}" if response['error'] puts "Error: #{response['error']}" elsif response.key?('result') result = response['result'] attempts = result['attempts'] url = result['url'] if attempts.zero? puts "Challenge failed. Attempts: #{attempts}" elsif url.nil? puts "Challenge succeeded but no URL available. Attempts: #{attempts}" else puts "Challenge succeeded. Attempts: #{attempts}" puts "URL with keyword: #{API_ENDPOINT}#{url}" end break end end
当前程序输出“Challenge succeeded but no URL available. Attempts: 1”,始终无法获取有效的URL值。
内容的提问来源于stack exchange,提问作者Wanyu Jiang
相关产品推荐
相关产品推荐

