Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:12:58+00:00 2026-06-15T16:12:58+00:00

I have the following Sinatra code: post ‘/bucket’ do # determine if this call

  • 0

I have the following Sinatra code:

post '/bucket' do
  # determine if this call is coming from filling out web form
  is_html = request.content_type.to_s.downcase.eql?('application/x-www-form-urlencoded')

  # If this is a curl call, then get the params differently
  unless is_html
      params = JSON.parse(request.env["rack.input"].read)
  end

  p params[:name]
end

If I call this using Curl, params has values, but when this is called via a web form, then params is nil and params[:name] has nothing. I spent several hours figuring out why it happens and asked help from other people, but no one could really find out what is going on.

One thing to note is, if I comment out this line:

params = JSON.parse(request.env["rack.input"].read)

then params has the correct value for “web-form” posting.

Actually, the goal is to get the params value if this code is being called by CURL call, so I used:

params = JSON.parse(request.env["rack.input"].read)

but it messed up the web-form posting. Can anyone solve this mystery?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:13:00+00:00Added an answer on June 15, 2026 at 4:13 pm

    Personally, I’d do it differently, by setting a hidden value in the form, something like:

    <input type="hidden" name="webform" value="true">
    

    then use it like:

    if (params['webform'])
      # this is a request from the form
    else
      # this is a request from Curl
    end
    

    If you see it you know the request came from your web-form. If params['webform'] doesn’t exist didn’t it came from Curl.

    I saved this to a file and ran it with Ruby:

    require 'sinatra'
    
    get '/bucket' do
      params[:name]
    end
    

    Calling the running script using http://localhost:4567/bucket?name=foo displays:

    foo
    

    in the browser.

    If I modify the source like:

    require 'sinatra'
    
    post '/bucket' do
      params[:name]
    end
    

    restart it and load a simple HTML file:

    <html>
      <body>
        <form name="form" method="post" action="http://localhost:4567/bucket">
          <input type="hidden" name="webform" value="true"></input>
          <input type="input" name="name"></input>
          <input type="submit"></input>
        </form>
      </body>
    </html>
    

    and input foobar and submit it, I see:

    foobar
    

    in the browser window.

    If I change the script to:

    require 'sinatra'
    
    post '/bucket' do
      if (params[:webform])
        'webform is set'
      else
        'webform is not set'
      end
    end
    

    and restart Sinatra and resubmit the form, I see:

    webform is set
    

    If I call it using Curl:

    curl --data "name=foo" http://127.0.0.1:4567/bucket
    

    I see this on the command-line as Curl’s response:

    webform is not set
    

    If I change the script to:

    require 'sinatra'
    
    post '/bucket' do
      if (params[:webform])
        'webform is set'
      else
        params[:name]
      end
    end
    

    restart the script, and call it with the Curl command again, I see:

    foo
    

    on the command line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following Sinatra code: get '/hi' do erb :hello end This
(I'm new to Sinatra) I have, in my index.erb the following form: <form method=post
Have following code. object.waypoint=function () { this.uw=setInterval( function() { console.log(this); }, 200); } How
I have the following .erb view in a Sinatra app: <% sessions.each do |session|
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I need to send a POST request from a sinatra app to a rails
I have the following rake file to create a static version of my sinatra
all, i need to do this as the following require 'sinatra' require 'app_env' get
I have following Code Block Which I tried to optimize in the Optimized section
I have following block of code ///////////////////////////////////// CComVariant newVal; //pass the CComVariant and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.