Skip to content

Instantly share code, notes, and snippets.

@thomthom
Created October 24, 2017 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomthom/8121168be4b8e8505f2828a3f80feb59 to your computer and use it in GitHub Desktop.
Save thomthom/8121168be4b8e8505f2828a3f80feb59 to your computer and use it in GitHub Desktop.
model_file = File.join(ENV['HOME'], 'Desktop', 'test.skp')
filename = File.basename(model_file)
# Build the POST body content:
BOUNDARY = "----RubyMultipartClient#{rand(1000000)}ZZZZZ"
data = []
data << "Content-Type: multipart/form-data; boundary=#{BOUNDARY}\r\n\r\n"
data << "--#{BOUNDARY}\r\n"
data << "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\"\r\n\r\n"
File.open(model_file, "rb:BINARY") { |file|
contents = file.read
data << contents
}
data << "\r\n\r\n--#{BOUNDARY}--\r\n"
# When we join the data array Ruby will choke if we have unicode strings
# along with the binary data. This happens when we have filenames with
# unicode characters. Because of this we force Ruby to treat all the strings
# as binary. Ruby alias BINARY as ASCII-8BIT which is a bit confusing. But
# encodings in Ruby isn't a pretty beast.
data.each { |line|
line.force_encoding(Encoding::BINARY)
}
payload = data.join
# Set the headers:
headers = {}
headers['Content-Type'] = "multipart/form-data; boundary=#{BOUNDARY}"
headers
url = 'http://posttestserver.com/post.php'
# Make the request:
request = Sketchup::Http::Request.new(url, Sketchup::Http::POST)
request.headers = headers
request.body = payload
request.start do |req, res|
# Not entering
p res
p res.status_code
puts res.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment