I am developing an api-like app that allows other applications to send requests which trigger a series of actions in the api-like application. I need to find a way to send large parameters (embed code) via url. I’m using Sinatra.
My first attempt: (Doesn’t work because embed code is not an acceptable url parameter)
get '/grab/:name/:bucket/:embed_code' do
@video = Video.create(
:name => params[:name],
:bucket => params[:bucket],
:embed_code => params[:embed_code],
:created_at => Time.now
)
redirect "/video/#{@video.id}"
end
My second attempt: (Connection refused - connect(2) (Errno::ECONNREFUSED) Error)
# (get '/video_form') points to (post '/video') and post_form was supposed to trigger it
get '/video_form' do
erb :new_video
end
post '/video' do
@video = Video.create(
:name => params[:name],
:bucket => params[:bucket],
:embed_code => params[:embed_code],
:created_at => Time.now
)
redirect "/video/#{@video.id}"
end
Net::HTTP.post_form(URI.parse('http://localhost:4567/video_form'),{'name'=>'example_2', 'bucket' => 'bucket_name', 'embed_code' => '<iframe width="560" height="315" src="http://www.youtube.com/embed/ncL1UlvjiMQ" frameborder="0" allowfullscreen></iframe>'})
A proper GET request might look like
http://localhost/getit?embed=%3Ciframe%20width%3D%22560%22%20height%3D%22315%22%20src%3D%22youtube.com/embed/ncL1UlvjiMQ%22%3B%20frameborder%3D%220%22%20allowfullscreen%3E%3C/iframe%3E. For more information, read up on Encoding Query String Parameters. Quoting:You can try this yourself via:
Notice that with this you don’t have to encode the value yourself; the web browser does that when it sends the form. Alternatively, you can make this GET request via JavaScript:
A POST request doesn’t show up on the query string, but is encoded by the browser. Take the same HTML as above and change the method, and watch Sinatra receive it.
To receive a complex parameter in Sinatra don’t try to make it part of the path, receive it as a proper parameter on the query string: