开发者

Ruby Sinatra simple app - Raw POST Data

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜