英文:
How do I get the value of URL from PUT request? In Ruby
问题
What I need is the value of URL from the PUT request.
Here is the result of the PUT request.
Response: {"result":{"attempts":1,"url":null}}
I can't tell where is the problem.
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
The thing I want to do,
Start a challenge by making a POST HTTP request to /challenges.
At the scheduled invocation time (actives_at) time in the response, an HTTP request is sent as PUT to /challenges. At this time, it is necessary to give the challenge ID (id) to the X-Challenge-Id header.
I tried to print out the error message but seems the challenge succeeded just no URL available.
英文:
What I need is the value of URL from the PUT request.
Here is the result of the PUT request.
Response: {"result":{"attempts":1,"url":null}}
I can't tell where is the problem.
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
The thing I want to do,
Start a challenge by making a POST HTTP request to/challenges.
At the scheduled invocation time (actives_at) time in the response, an HTTP request is sent as PUT to/challenges. At this time, it is necessary to give the challenge ID (id) to the X-Challenge-Id header.
I tried to print out the error message but seems the challenge succeeded just no URL available.
答案1
得分: 1
需要将字符串解析为哈希的 JSON 数据
json_response = HTTParty.put("#{API_ENDPOINT}/challenges", headers: { 'X-Challenge-Id' => challenge_id })
response = JSON.parse(json_response.body, symbolize_names: true)
解析后,您可以处理响应
if response[:error]
puts "Error: #{response[:error]}"
elsif response[:result]
attempts = response[:result][:attempts]
url = response[: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
end
英文:
You need parse JSON from string to hash
json_response = HTTParty.put("#{API_ENDPOINT}/challenges", headers: { 'X-Challenge-Id' => challenge_id })
response = JSON.parse(json_response.body, symbolize_names: true)
After that you can process response
if response[:error]
puts "Error: #{response[:error]}"
elsif response[:result]
attempts = response[:result][:attempts]
url = response[: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
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论