[RUBY] I tried a puzzle that can only be solved by the bottom 10% of bad engineers

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

Puzzle

First, 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 heard that it is not GET, so Let's request by POST.

You can do it with curl, but this time I will try to 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 instructed to pass the parameters. Since it is POST, data is put in 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? I can't pass the parameters, From here, ponder, and cheat. Lol

The cause was that I put the parameters in json format in the request body. I regret that I thought that the body was json ...

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

require 'net/http'
params = {
   name: 'hogehoge'
}
#abridgement
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\"]"}

You can't specify parameters forever, right? 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 wondering if it's true I entered another address and it succeeded.

{"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 position, saying that I am still in the bottom 10%. Now that I have the knowledge to insert request parameters regarding POST, I think it's a step forward.

Dedicated every day.

Recommended Posts

I tried a puzzle that can only be solved by the bottom 10% of bad engineers
A description that only the poster can access
The strongest Omikuji app that only I, an EKS Lover, can think of
A scraping of past weather that can be seen on the Japan Meteorological Agency website
I tried JAX-RS and made a note of the procedure
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I made a question that can be used for a technical interview
[Java] I tried to make a maze by the digging method ♪
Mecab installation that can be done almost by typing a command
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I tried learning Java with a series that beginners can understand clearly
I separated the same description as a method under private in Rails controller and implemented it so that it can be called by before_action
The function that can be divided and assigned by right assignment of the development version of Ruby 3.0 has been added.
I want to get a list of only unique character strings by excluding fixed character strings from the file name
Object-oriented that can be understood by fairies
[Android] I want to create a ViewPager that can be used for tutorials
What I tried when I wanted to get all the fields of a bean
I tried to compare the infrastructure technology of engineers these days with cooking.
I made a THETA API client that can be used for plug-in development
Try to save the data that can be read by JavaFX as PNG
[Must-see for fledgling engineers] A story that the world expanded when I actually operated a web service that I made by myself
Graph the sensor information of Raspberry Pi and prepare an environment that can be checked with a web browser
A collection of RSpecs that I used frequently
I tried using the profiler of IntelliJ IDEA
[Ruby] I want to make a program that displays today's day of the week!
I tried to create a log reproduction script at the time of apt install
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried to operate home appliances by holding a smartphone over the NFC tag
[VBA] I tried to make a tool to convert the primitive type of Entity class generated by Hibernate Tools to the corresponding reference type.