开发者

Ruby Sinatra simple app - Raw POST Data

开发者 https://www.devze.com 2023-03-06 02:27 出处:网络
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.j

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号