-
-
Save thomthom/8121168be4b8e8505f2828a3f80feb59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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