I am learning Sinatra and Redis building a small URL Shortener.
Here is my POST
route:
helpers do
include Rack::Utils
alias_method :h, :escape_html
def random_string(length)
rand(36**length).to_s(36)
end
end
post '/' do
if params[:url].present?
content_type :json
@shortcode = random_string 5
short_url = redis.setnx "links:#{@shortcode}", params[:url]
short_url.to_json
end
end
This POST method will take a long URL as input and generate a short one as output in jSON format.
But, when I try to do this with cURL using
curl -H "Content-Type: application/json" -X POST -d '{"url": "http://mywebsite.com"}' http://localhost:4567
it fails and I get
NoMethodError: undefined method `present?' for nil:NilClass.
Any hint?