-
-
Save khun84/03e3c173908fe760f93ba4294ed01b02 to your computer and use it in GitHub Desktop.
Ruby rest-client file upload as multipart
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
| filepath = 'local-filepath' | |
| url = 'web-url' | |
| File.open(filepath, 'w') {|f| | |
| block = proc { |response| | |
| response.read_body do |chunk| | |
| puts "Working on response" | |
| f.write chunk | |
| end | |
| } | |
| RestClient::Request.new(method: :get, url: url, block_response: block).execute | |
| } |
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 'rest-client' | |
| RestClient.get(url, headers={}) | |
| RestClient.post(url, payload, headers={}) |
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
| RestClient.get 'http://example.com/resource' |
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
| RestClient.post "http://example.com/resource", {'x' => 1}.to_json, {content_type: :json, accept: :json} |
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
| RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb') |
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
| RestClient.post '/data', {:foo => 'bar', :multipart => true} |
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
| request = RestClient::Request.new( | |
| :method => :post, | |
| :url => '/data', | |
| :user => @sid, | |
| :password => @token, | |
| :payload => { | |
| :multipart => true, | |
| :file => File.new("/path/to/image.jpg", 'rb') | |
| }) | |
| response = request.execute |
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
| response = RestClient.post( | |
| '/data', | |
| { | |
| :name => 'daipresents', | |
| :user_image => File.new('sample.jpg', 'rb') | |
| } | |
| ) |
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
| request = RestClient::Request.new( | |
| :method => :post, | |
| :url => '/data', | |
| :payload => { | |
| :name => 'daipresents', | |
| :user_image => File.new('sample.jpg', 'rb') | |
| }) |
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
| RestClient.post( url, | |
| { | |
| :transfer => { | |
| :path => '/foo/bar', | |
| :owner => 'that_guy', | |
| :group => 'those_guys' | |
| }, | |
| :upload => { | |
| :file => File.new(path, 'rb') | |
| } | |
| }) |
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
| RestClient.post "http://example.com/resource", {'x' => 1}.to_json, {content_type: :json, accept: :json} |
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
| RestClient.get 'https://user:[email protected]/private/resource', {accept: :json} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment