i am working on file upload into box.com but i am stuck there. i used Net::HTTP for that and i need the help of regarding this library.
my main code which interact to box.com is
module BoxApi
class FileOperation < Base
attr_reader :upload_path
def initialize
super
@upload_path = "https://upload.box.com/api/2.0/files/content"
end
#here filder_id args denote folder inside upload file and file args hold the content of file which are uploaded by file_field
def upload(folder_id, file)
boundary = "AaB03x"
body = []
body << "--#{boundary}\r\n"
body << "Content-Disposition: form-data; name='filename'; filename=#{file.original_filename}\r\n"
body << "Content-Type: text/plain\r\n\r\n"
body << file
body << "--#{boundary}\r\n"
body << "Content-Disposition: form-data; name='parent_id'"
body << "\r\n"
body << folder_id
body << "\r\n--#{boundary}--\r\n"
https_post(URI.parse("#{upload_path}"), body, boundary)
# `curl "Authorization: Bearer MlaNbyAefUWrawZEqGkDKvq9foCmQ0lL" -F filename=@./public/404.html -F parent_id='#{folder_id}' #{upload_path}`
rescue => ex
p "Exception caught (1) ==> #{ex}"
end
private
def https_post(uri, body, boundary)
http = https_setting(uri)
request = Net::HTTP::Post.new(uri.request_uri)
# request.body = JSON.parse(body)
request.body = body.join
request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
request["Authorization"] = "Bearer #{box_token.token}"
http.request(request)
rescue => ex
p "Exception caught (2) ==> #{ex}"
end
def https_setting(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http
end
end
end