Skip to content

Instantly share code, notes, and snippets.

@geemus
Last active July 16, 2023 08:36
Show Gist options
  • Save geemus/8198572 to your computer and use it in GitHub Desktop.
Save geemus/8198572 to your computer and use it in GitHub Desktop.
require 'excon'
require 'securerandom'
def multipart_form_data(buildpack_file_path)
body = ''
boundary = SecureRandom.hex(4)
data = File.open(buildpack_file_path)
data.binmode if data.respond_to?(:binmode)
data.pos = 0 if data.respond_to?(:pos=)
body << "--#{boundary}" << Excon::CR_NL
body << %{Content-Disposition: form-data; name="buildpack"; filename="#{File.basename(buildpack_file_path)}"} << Excon::CR_NL
body << 'Content-Type: application/x-gtar' << Excon::CR_NL
body << Excon::CR_NL
body << File.read(buildpack_file_path)
body << Excon::CR_NL
body << "--#{boundary}--" << Excon::CR_NL
{
:headers => { 'Content-Type' => %{multipart/form-data; boundary="#{boundary}"} },
:body => body
}
end
form_data = multipart_form_data(__FILE__)
# debug output
#puts
#form_data[:headers].each do |k,v|
#puts "#{k}: #{v}"
#end
#puts
#puts form_data[:body]
# rough sample usage
# connection = Excon.new(...)
# connection.request(
# :body => form_data[:body],
# :headers => form_data[:headers],
# :path => ...,
# )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment