Created
April 1, 2013 18:53
-
-
Save vparihar01/5286863 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| require "net/http" | |
| require "uri" | |
| require 'json' | |
| # Token used to terminate the file in the post body. Make sure it is not | |
| # present in the file you're uploading. | |
| BOUNDARY = "goBonziteam" | |
| JSON_RESPONSE = ActiveSupport::JSON.decode('{ | |
| "args": { | |
| "values": { | |
| "Sms.sendText": "true", | |
| "cSubject": "BonziTeam Alert", | |
| "cText": "hello27thmarch" | |
| }, | |
| "options": { | |
| "queryType": "insert", | |
| "mobileAppVersion": "1.0" | |
| }, | |
| "itemType": "Sms" | |
| } | |
| }') | |
| pust JSON_RESPONSE | |
| uri = URI.parse("https://www.bonziteam.com/api.php") | |
| post_body = [] | |
| post_body << "-- #{BOUNDARY}\r\n" | |
| post_body << "Content-Disposition: form-data; name=\"cmd\";\r\n" | |
| post_body << "tsql" | |
| post_body << "\r\n--#{BOUNDARY}--\r\n" | |
| post_body << "-- #{BOUNDARY}\r\n" | |
| post_body << "Content-Disposition: form-data; name=\"args\";\r\n" | |
| post_body << JSON_RESPONSE.to_json | |
| post_body << "\r\n--#{BOUNDARY}--\r\n" | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| http.use_ssl = true | |
| http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
| http.set_debug_output($stdout) | |
| # or | |
| http.set_debug_output($stderr) | |
| # or | |
| require "logger" | |
| http.set_debug_output(Logger.new("/var/log/my.log")) | |
| request = Net::HTTP::Post.new(uri.request_uri) | |
| request.body = post_body.join | |
| request["Content-Type"] = "multipart/form-data, boundary=#{BOUNDARY}" | |
| request['Cookie'] = "PHP_SSL_TS=utl37kdq8mn1b6i95d5h7qr5r4; path=/" | |
| puts request | |
| puts request.body | |
| res = http.request(request) | |
| puts res.body | |
| require "net/http" | |
| require "uri" | |
| require 'json' | |
| boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ' | |
| parts = [] | |
| streams = [] | |
| @file_names.each do |param_name| | |
| parts << StringPart.new ( "--" + boundary + "\r\n" + | |
| "Content-Disposition: form-data; name=\"" + param_name.to_s + "\";"\r\n") | |
| stream = File.open(filepath, "rb") | |
| streams << stream | |
| parts << StreamPart.new (stream, File.size(filepath)) | |
| end | |
| parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" ) | |
| post_stream = MultipartStream.new( parts ) | |
| url = URI.parse( to_url ) | |
| req = Net::HTTP::Post.new(url.path) | |
| req.content_length = post_stream.size | |
| req.content_type = 'multipart/form-data; boundary=' + boundary | |
| req.body_stream = post_stream | |
| res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } | |
| # Alternative method, using Nick Sieger's multipart-post gem | |
| require "rubygems" | |
| require "net/http/post/multipart" | |
| reqest = Net::HTTP::Post::Multipart.new uri.request_uri, "file" => UploadIO.new(file, "application/octet-stream") | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| http.request(request) | |
| # Another alternative, using Rack 1.3 + | |
| require 'rack' | |
| uri = URI.parse("http://something.com/uploads") | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| request = Net::HTTP::Post.new(uri.request_uri) | |
| request.body = Rack::Multipart::Generator.new( | |
| "form_text_field" => "random text here", | |
| "file" => Rack::Multipart::UploadedFile.new(path_to_file, file_mime_type) | |
| ).dump | |
| request.content_type = "multipart/form-data, boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}" | |
| http.request(request) | |
| http.start do |connection| | |
| response = retrying_request(connection, request) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment