BuddyJoe
BuddyJoe

Reputation: 71131

Ruby Sinatra simple app - Raw POST Data

I'd like to setup a simple Sinatra app up to capture the raw POST data that gets sent to the the / URL and save this data to the file system as a file with the format YYYYMMDD-HHMMSS.json.

The data I will be posting to the URL with be simple text data in the JSON format.

What is the best way to set this simple Sinatra app up? Unsure how to capture the raw POST data.

UPDATE / Code:

post '/' do
    raw = request.env["rack.input"].read
    n = DateTime.now
    filename = n.strftime("%Y%m%d") + "T" + n.strftime("%H%M%S") #any way to include microseconds?
    # write to file
end

Upvotes: 2

Views: 4613

Answers (1)

intellidiot
intellidiot

Reputation: 11228

Something like this should work for you:

post "/" do
  File.open("#{Time.now.strftime("%Y%m%d-%H%M%S")}.json", "w") do |f| 
    f.puts params["data"]    
  end 
end

Upvotes: 5

Related Questions