Please tell me how to separate the content-type processing in grape.

Asked 2 years ago, Updated 2 years ago, 74 views

I am creating an API using grape.
(I don't use other frameworks such as ruby on rails or sinatora .grape alone.)

Check the content-type of the http header in the request and determine if the value is
application/x-www-form-urlencoded. I'd like to cut through the process, but I don't know how to do it.

The header ['Content-Type'] could not retrieve the value.

A solution using the rack mechanism ("You can write like this on config.ru") is fine.

ruby grape rack

2022-09-30 20:21

1 Answers

Self-resolved.

class MyRackMiddleware

  def initialize (app)
    @app=app
 end

  def call(env)
     unless env ["CONTENT_TYPE"] == 'application/x-www-form-urlencoded'
       raise
     end
    @ app.call(env)
  end

end

↑ I could have created middleware like this and called it inside config.ru.

use MyRackMiddleware
run API


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.