0
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 1 year has passed since last update.

RubyでProxy経由でファイルをPOSTする時のサンプル

Last updated at Posted at 2023-04-14

RubyでProxy経由でファイルをPOSTする時のサンプル。

ステージング環境がプロキシ経由じゃないと接続できない&Null Byte Injectionの検証のため、NULLを持ったファイルパスを送信する必要があり、cURLではなくプログラムからファイルを送信する必要があったため。

$ ruby --version
ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-darwin20]
require("net/http")
require("uri")

uri = URI.parse("http://localhost:8080") # 接続先のURLはダミーです
uri.path = '/testpath'

image_file = File.open("image_file_path.png", "rb")
begin
  data = [
    [ "image_file", image_file, { filename: "image_file_path.png", content_type: "image/png" } ]
  ]

  proxy = Net::HTTP::Proxy("111.111.111.111", 1111) # プロキシはダミーです
  https = proxy.new(uri.host, uri.port)
  https.use_ssl = true # HTTPSで接続する場合

  req = Net::HTTP::Post.new(uri.path)
  req.set_form(data, "multipart/form-data")

  res = https.start do
    https.request(req)
  end
  puts res
ensure
  image_file.close
end

HTTPS接続の設定わすれてて少しハマった。。

0
0
0

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
0
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?