Ruby - 处理传入的 HTTP POST?

标签 ruby heroku web-development-server

我似乎缺少一些基本的东西,因为我不知道如何捕获然后处理传入的 HTTP Post。我是第一次学习 Ruby 的 C/C++ 开发人员,我这辈子从来没有接触过 web 的东西。

我正在编写我的第一个 Slack Webhook,Slack 会将类似于此的数据发送到我的网络服务器:

token=ZLAmT1sKurm2KwvmYDR9hbiV
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678

我不知道如何接受这个 POST 请求,并将其发送到我的应用程序进行处理。

我假设它类似于处理终端应用程序的 argvargc,但我一直在写前几行。

我已经搜索了解决方案,但似乎我问错了问题。

我目前有一个在 heroku 上运行的 Puma/Sinatra 网络服务器,Procfile 包含:

web: bundle exec puma -p $PORT(我也不知道分配给它的端口是什么。)

最佳答案

类似于this answer , Sinatra 提供了一个 DSL 来接受 POST 请求。在 block 内,数据可以在 params 散列中访问。有点像

post '/my_endpoint' do
  content_type :json
  @team_id = params[:team_id]
  res = do_stuff_with_channel_id params[:channel_id] # passing the value to a custom method example
  {my_response: res}.to_json #simple example of returning JSON in response 
end

Sinatra Docs on routing

HTH

关于Ruby - 处理传入的 HTTP POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38005784/

相关文章:

Ruby 在使用 thin+ssl 时在 Windows 上崩溃

go - 如何使用 `dep` 将带有内部包的 Go 应用程序部署到 Heroku

python - Heroku - Django : Had to change every mentioning of `myproject` into `app` to get my site working. 将来如何最好地避免这种情况?

python - 使用 Redis 添加重复任务

javascript - socket.io,使用网络套接字的聊天应用程序 - 关闭 tab1 会导致 tab2 和 tab3 中的 void "online users"

java - 玩!框架链接

ruby-on-rails - 事件记录 : create record from parent object

ruby-on-rails - 获取复选框的值 [Ruby/Sinatra(Rails)]

ruby-on-rails - ruby rails : Search Form - multiple search fields

python - 如何设置 Bokeh 应用程序以便可以通过互联网访问它?