如何从Ruby的PUT请求中获取URL的值?

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月13日 17:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定