我似乎缺少一些基本的东西,因为我不知道如何捕获然后处理传入的 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 请求,并将其发送到我的应用程序进行处理。
我假设它类似于处理终端应用程序的 argv
和 argc
,但我一直在写前几行。
我已经搜索了解决方案,但似乎我问错了问题。
我目前有一个在 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
HTH
关于Ruby - 处理传入的 HTTP POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38005784/