Skip to content

Instantly share code, notes, and snippets.

@daipresents
Last active May 22, 2021 03:01
Show Gist options
  • Save daipresents/528891497cc6e89f5c06e597a8dcc2e1 to your computer and use it in GitHub Desktop.
Save daipresents/528891497cc6e89f5c06e597a8dcc2e1 to your computer and use it in GitHub Desktop.
Ruby rest-client file upload as multipart
require 'rest-client'
RestClient.get(url, headers={})
RestClient.post(url, payload, headers={})
RestClient.get 'http://example.com/resource'
RestClient.post "http://example.com/resource", {'x' => 1}.to_json, {content_type: :json, accept: :json}
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
RestClient.post '/data', {:foo => 'bar', :multipart => true}
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
response = RestClient.post(
'/data',
{
:name => 'daipresents',
:user_image => File.new('sample.jpg', 'rb')
}
)
request = RestClient::Request.new(
:method => :post,
:url => '/data',
:payload => {
:name => 'daipresents',
:user_image => File.new('sample.jpg', 'rb')
})
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path, 'rb')
}
})
RestClient.post "http://example.com/resource", {'x' => 1}.to_json, {content_type: :json, accept: :json}
RestClient.get 'https://user:password@example.com/private/resource', {accept: :json}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment