[Ruby] I tried a puzzle that only the bottom 10% of bad engineers can’t solve

1 minute read

A puzzle like the one in the title was popular several years ago, I found it, so I challenged.

Puzzle

First of all, try typing directly into the browser. http://challenge-your-limits.herokuapp.com/call/me Then, the following text came back.

{"message":"Almost! It's not GET. Keep trying."}

I’m sure it’s not GET, so Let’s request by POST.

You can do it with curl, but this time I will make a request with Ruby.

require'net/http'
uri = URI.parse('http://challenge-your-limits.herokuapp.com/call/me')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
response = http.request(req)
puts response.body
#=>{"message":"Great! Please register as /challenge_users"}

Success, and the next challenge First, change the path and request as it is.

require'net/http'
uri = URI.parse('http://challenge-your-limits.herokuapp.com/challenge_users')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
response = http.request(req)
puts response.body
#=>{"message":"Validation Error, [:name, \"can't be blank\"]"}

I was able to pass parameters and received instructions. Since it is POST, insert data into the request body.

require'net/http'
require'json'
params = {
   name:'hogehoge'
}
uri = URI.parse('http://challenge-your-limits.herokuapp.com/challenge_users')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
req.body = params.to_json
response = http.request(req)
puts response.body
#=> {"message":"Validation Error, [:name, \"can't be blank\"]"}

that? No parameters can be passed, From here, ponder, and cheat. Lol

The cause was that the request body contains parameters in json format. I thought that the body was json, but…

The Net::HTTP::Post library has a convenience method called set_form_data, If you pass hash as an argument, it will put the parameter in the body as a string.

require'net/http'
params = {
   name:'hogehoge'
}
# Omitted
req.set_form_data(params)
puts req.body
#=> name=hogehoge
puts req.body.class
#=> String

And edit

require'net/http'
params = {
   name:'hogehoge'
}
uri = URI.parse('http://challenge-your-limits.herokuapp.com/challenge_users')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(params)
response = http.request(req)
puts response.body
#=> {"message":"Validation Error, [:email, \"can't be blank\"]"}

Doesn’t parameter specification last forever? Add email as instructed

require'net/http'
params = {
   name:'hogehoge',
   email:'[email protected]'
}
uri = URI.parse('http://challenge-your-limits.herokuapp.com/challenge_users')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(params)
response = http.request(req)
puts response.body
#=> {"message":"Validation Error, [:email, \"is already taken\"]"}

uniq: While thinking whether it is true, If you enter another address, it was successful.

{"message":"Thanks! Please access to http://challenge-your-limits.herokuapp.com/challenge_users/token/********** from your web browser."}

It was a good opportunity to reconfirm my standing position, as I was still in the bottom 10%. I got knowledge about how to insert request parameters for POST, so I think it’s a step forward.

Devot every day.

Tags: ,

Updated: