DEV Community

KAK
KAK

Posted on • Originally published at railsmine.net on

Ruby Roda Web Application Framework Post Request Params

There is difference when accessing the parameters data in Roda aplication between sending request withContent-Type: application/x-www-form-urlencoded and Content-Type: application/json.

When Sending POST Request with Content-Type: x-www-form-urlencoded

Use r.params['your_params'] to read your_params parameter value.

Example Request

curl -v -X POST --data "foobar=example_value" example.com/foo/bar

Example Roda Route

class Example
  route 'foo' do |r|
    r.post 'bar' do
      r.params['foobar'] # => example_value
    end
  end
end

When Sending POST Request with Content-Type: application/json

Use r.body.read to read the JSON body parameter and parse it with JSON parser.

Example Request

curl -v -X POST -H "Content-Type: application/json" -d '{"foobar":"example_value}' "example.com/foo/bar"

Example Roda Route

class Example
  route 'foo' do |r|
    r.post 'bar' do
      params = JSON.parse(r.body.read) # => {"foobar"=>"example_value"}
    end
  end
end

Top comments (0)