1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby Sinatraサーバーにhttpclientからファイルアップロードしてみた

Last updated at Posted at 2019-04-25

すごく簡単に書けたけど、忘れそうなので書いておく。

なおこの記事では、ファイルアップロードの方法を簡潔に記載するため、ディレクトリトラバーサルなどの脅威への対策は一切行っていません。参考にされる方はご注意ください。

server.rb
require 'sinatra'

set :port, 3000

post '/upload' do
  halt 400, 'please upload file' if params[:file].nil?
  path = "./public/#{params[:file][:filename]}".freeze
  File.open(path, 'wb') { |f| f.write params[:file][:tempfile].read }
  'ok'
end
client.rb
require 'httpclient'

c = HTTPClient.new
url = 'http://localhost:3000/upload'.freeze
path = ARGV.first
c.post(url, file: File.open(path, 'r'))
1
0
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?