Skip to content

Instantly share code, notes, and snippets.

@tvon
Created August 29, 2011 20:42
Show Gist options
  • Save tvon/1179359 to your computer and use it in GitHub Desktop.
Save tvon/1179359 to your computer and use it in GitHub Desktop.
raw api file upload
#!/usr/bin/env ruby
require "net/http"
require "uri"
url = 'http://example.com/submissions.json'
token = 'TOKEN HERE'
boundary = "pop-pop"
uri = URI.parse(url)
file = "spec/fixtures/sample.jpg"
body = []
body << %{--#{boundary}\r\n}
body << %{Content-Disposition: form-data; name="auth_token"\r\n}
body << %{\r\n}
body << token
body << "\r\n--#{boundary}\r\n"
body << %{Content-Disposition: form-data; name="submission[media_file]"; filename="#{File.basename(file)}"\r\n}
body << %{Content-Type: image/jpeg\r\n}
body << %{\r\n}
body << File.read(file)
body << %{\r\n--#{boundary}\r\n}
body << %{Content-Disposition: form-data; name="submission[description]"\r\n}
body << %{\r\n}
body << %{Sample%20Description}
body << %{\r\n--#{boundary}\r\n}
body << %{Content-Disposition: form-data; name="submission[assignment_id]"\r\n}
body << %{\r\n}
body << %{12}
body << %{\r\n--#{boundary}--\r\n}
body = body.join
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.body = body
request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
request["Accept"] = "application/json"
result = http.request(request)
puts result.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment