I'd like to setup a simple Sinat开发者_开发技巧ra 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
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
精彩评论