NictraSavios
NictraSavios

Reputation: 519

Ruby - Handle incoming HTTP POST?

I seem to be missing something basic, as I don't know how to capture and then handle an incoming HTTP Post. I'm a C/C++ developer learning Ruby for the first time, and I've never dealt with web-stuff in my life.

I'm writing my first Slack Webhook, and slack will send data similar to this to my webserver:

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

I have no idea how to accept this POST request, and send it to my application for processing.

I assume it's similar to handling argv and argc for a terminal application, but I'm stuck writing the first few lines.

I've searched for a solution, but it seems like I'm asking the wrong questions.

I currently have a Puma/Sinatra webserver running on heroku, with the Procfile containing:

web: bundle exec puma -p $PORT (I also have no idea what port is assigned to it.)

Upvotes: 2

Views: 107

Answers (1)

maxbeizer
maxbeizer

Reputation: 857

Similar to this answer, Sinatra provides a DSL to accept POST requests. Within the block, the data will be accessible in the params hash. Something like

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

Upvotes: 1

Related Questions