Skip to content

Instantly share code, notes, and snippets.

@minase
Created June 10, 2009 09:25
Show Gist options
  • Save minase/127115 to your computer and use it in GitHub Desktop.
Save minase/127115 to your computer and use it in GitHub Desktop.
def build_multipart_post_data(params, boundary)
enter = "\r\n"
data = ''
params.each do |k,v|
next if v.nil?
data << '--' + boundary + enter
data << %Q[Content-Disposition: form-data; name="#{k.to_s}"]
if k.to_s == 'file'
filename = v.split('/').last
mimetype = file_type[v.split('.').last]
data << %Q[; filename="#{filename}"] + enter
data << %Q[Content-Type: application/octet-stream] + enter + enter
data << File.read(v) + enter
else
data << enter + enter
data << v + enter
end
end
data << '--' + boundary + '--'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment