A puzzle like the one in the title was popular a few years ago. I found it, so I challenged it.
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