Date   

Using restclient for streaming multipart uploads

Anthony Rowlands <draftomatic@...>
 

Hello RestClient list,

I am trying to use the RestClient library to do a multipart upload from a StringIO, not from a file. After scouring the code, it seems that this is not possible. It should be something like this (although this example is obviously wrong):

response = RestClient.post(url, :payload => {
        :multipart => true,
        :content_type => 'multipart/form-data',
        :source => myStringIO
      }, :headers => {
          :cookies => {
            'JSESSIONID' => @sessionid
          }
        }
      )

In payload.rb, in Multipart.build_stream, v is the StringIO I passed to RestClient.post, and it leads to a call to create_file_field (StringIO seems to respond to :path even though this isn't documented in the rubydocs), but StringIO.path is always nil for me, which is causing problems:

Error: #<TypeError: can't convert nil into String>
C:/PROGRA~2/Google/GOOGLE~2/Plugins/Light Stanza/lib/restclient/payload.rb:202:in `basename'
C:/PROGRA~2/Google/GOOGLE~2/Plugins/Light Stanza/lib/restclient/payload.rb:202:in `create_file_field'
C:/PROGRA~2/Google/GOOGLE~2/Plugins/Light Stanza/lib/restclient/payload.rb:179:in `build_stream'

Can anyone help me with this? Is it possible to do a streaming multipart post from a StringIO?

Thanks,
Anthony R


Re: How To See the Request URL?

Keith Bennett <keithrbennett@...>
 

Thanks guys!

That's perfect.

- Keith

On Sat, Oct 29, 2011 at 7:51 PM, will leinweber <will@...> wrote:
yeah, RESTCLIENT_LOG is the env var. The great thing about that is you can
use that infront of any other gems that happen to use restclient
$ RESTCLIENT_LOG=stdout heroku addons:list

RestClient.get "https://api.heroku.com/addons",
"Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate",
"User-Agent"=>"heroku-gem/2.8.6", "X-Heroku-API-Version"=>"2",
"X-Ruby-Platform"=>"x86_64-darwin11.0.1", "X-Ruby-Version"=>"1.9.2"
2011/10/29 François Beausoleil <francois@...>

require "logger"
RestClient.log = Logger.new(STDOUT)

That should get you most of the way there. There's also an environment
variable you can use (maybe RESTCLIENT_LOG?) which does the same thing.

Hope that helps!


Le samedi 29 octobre 2011 à 15:27, Keith Bennett a écrit :

Hello, I'm just starting to use rest-client.

I'm not getting back what I expected when I call RestClient.get. It
would be helpful to see the URL that is being sent. Is there any way
for me to see that?

Thanks,
Keith


Re: How To See the Request URL?

will leinweber <will@...>
 

yeah, RESTCLIENT_LOG is the env var. The great thing about that is you can use that infront of any other gems that happen to use restclient

$ RESTCLIENT_LOG=stdout heroku addons:list                                   
RestClient.get "https://api.heroku.com/addons", "Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"heroku-gem/2.8.6", "X-Heroku-API-Version"=>"2", "X-Ruby-Platform"=>"x86_64-darwin11.0.1", "X-Ruby-Version"=>"1.9.2"

2011/10/29 François Beausoleil <francois@...>

require "logger"
RestClient.log = Logger.new(STDOUT)

That should get you most of the way there. There's also an environment variable you can use (maybe RESTCLIENT_LOG?) which does the same thing.

Hope that helps!


Le samedi 29 octobre 2011 à 15:27, Keith Bennett a écrit :

> Hello, I'm just starting to use rest-client.
>
> I'm not getting back what I expected when I call RestClient.get. It
> would be helpful to see the URL that is being sent. Is there any way
> for me to see that?
>
> Thanks,
> Keith





Re: How To See the Request URL?

"François Beausoleil <francois@...>
 

require "logger"
RestClient.log = Logger.new(STDOUT)

That should get you most of the way there. There's also an environment variable you can use (maybe RESTCLIENT_LOG?) which does the same thing.

Hope that helps!

Le samedi 29 octobre 2011 à 15:27, Keith Bennett a écrit :

Hello, I'm just starting to use rest-client.

I'm not getting back what I expected when I call RestClient.get. It
would be helpful to see the URL that is being sent. Is there any way
for me to see that?

Thanks,
Keith


How To See the Request URL?

Keith Bennett <keithrbennett@...>
 

Hello, I'm just starting to use rest-client.

I'm not getting back what I expected when I call RestClient.get. It
would be helpful to see the URL that is being sent. Is there any way
for me to see that?

Thanks,
Keith


Re: BDD with REST Client

Archiloque <code@...>
 


Le 16 oct. 2011 à 22:04, Kunal Shah a écrit :

Hello all,

I'm building a Ruby client for our very new, very alpha alpha API (at developer.urtak.com) and would like to attack it with BDD.

Since I know I'll be using REST Client as my HTTP client, I was looking for some resources on using it in my specs as well.

I'm debating making actual calls against the API or stubbing them out with something like FakeWeb. Advice?

Kunal

I never used FakeWeb but I used webmock and found it better than directly using the API directly since it better describes the contract the tests are supposed to check, see
for example

A.



BDD with REST Client

Kunal Shah <me@...>
 

Hello all,

I'm building a Ruby client for our very new, very alpha alpha API (at developer.urtak.com) and would like to attack it with BDD.

Since I know I'll be using REST Client as my HTTP client, I was looking for some resources on using it in my specs as well.

I'm debating making actual calls against the API or stubbing them out with something like FakeWeb. Advice?

Kunal


Re: Trying to figure out an elegant way to handle basicauth

Brian Gupta <brian.gupta@...>
 

In case anyone wants to know how this turned out, thanks to Archiloque's help, I got it working with:

def get_collection(path)
#RestClient.log = 'stdout'
  response = RestClient::Request.new(:method => :get, :url => @my_url + "/" + path.to_s, :user => @my_user, :password => @my_pass, :headers => { :accept => :json, :content_type => :json }).execute
  results = JSON.parse(response.to_str)
end

-Brian


On Sat, Oct 8, 2011 at 1:06 PM, Archiloque <code@...> wrote:
sorry:

 try RestClient::Request.new(......).execute

I'm on ruby's IRC if you have other questions

A.


Le 8 oct. 2011 à 19:02, Brian Gupta a écrit :

(In way of background, I am learning ruby for this project). I guess the issue is I don't understand what "Request.execute" is, or how to actually call it? I tried plugging it in as replacement for RestClient.get but didn't work (error follows below the example). IE:

def get_collection(path)
    response = Request.execute(:method => :get, :url => @my_url, :login => @my_user, :password => @my_pass, :headers => { :accept => :json, :content_type => :json })
    results = JSON.parse(response.to_str)
end

error:

in `get_collection': uninitialized constant Request (NameError)

That still leaves the path (including query string) as unhandled, but I figure one step at a time.

Thanks,
Brian
On Sat, Oct 8, 2011 at 12:40 PM, Archiloque <code@...> wrote:

I don't think, do you have any question about the API ?

A.


Cool and thank you. Is there a more fleshed out example of this somewhere?

Thanks,
Brian

On Sat, Oct 8, 2011 at 5:02 AM, Archiloque <code@...> wrote:
Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 






--






--






--



Re: Trying to figure out an elegant way to handle basicauth

Archiloque <code@...>
 

sorry:

 try RestClient::Request.new(......).execute

I'm on ruby's IRC if you have other questions

A.


Le 8 oct. 2011 à 19:02, Brian Gupta a écrit :

(In way of background, I am learning ruby for this project). I guess the issue is I don't understand what "Request.execute" is, or how to actually call it? I tried plugging it in as replacement for RestClient.get but didn't work (error follows below the example). IE:

def get_collection(path)
    response = Request.execute(:method => :get, :url => @my_url, :login => @my_user, :password => @my_pass, :headers => { :accept => :json, :content_type => :json })
    results = JSON.parse(response.to_str)
end

error:

in `get_collection': uninitialized constant Request (NameError)

That still leaves the path (including query string) as unhandled, but I figure one step at a time.

Thanks,
Brian
On Sat, Oct 8, 2011 at 12:40 PM, Archiloque <code@...> wrote:

I don't think, do you have any question about the API ?

A.


Cool and thank you. Is there a more fleshed out example of this somewhere?

Thanks,
Brian

On Sat, Oct 8, 2011 at 5:02 AM, Archiloque <code@...> wrote:
Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 






--






--




Re: Trying to figure out an elegant way to handle basicauth

Brian Gupta <brian.gupta@...>
 

(In way of background, I am learning ruby for this project). I guess the issue is I don't understand what "Request.execute" is, or how to actually call it? I tried plugging it in as replacement for RestClient.get but didn't work (error follows below the example). IE:

def get_collection(path)
    response = Request.execute(:method => :get, :url => @my_url, :login => @my_user, :password => @my_pass, :headers => { :accept => :json, :content_type => :json })
    results = JSON.parse(response.to_str)
end

error:

in `get_collection': uninitialized constant Request (NameError)

That still leaves the path (including query string) as unhandled, but I figure one step at a time.

Thanks,
Brian

On Sat, Oct 8, 2011 at 12:40 PM, Archiloque <code@...> wrote:

I don't think, do you have any question about the API ?

A.


Cool and thank you. Is there a more fleshed out example of this somewhere?

Thanks,
Brian

On Sat, Oct 8, 2011 at 5:02 AM, Archiloque <code@...> wrote:
Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 






--






--



Re: Trying to figure out an elegant way to handle basicauth

Archiloque <code@...>
 


I don't think, do you have any question about the API ?

A.


Cool and thank you. Is there a more fleshed out example of this somewhere?

Thanks,
Brian

On Sat, Oct 8, 2011 at 5:02 AM, Archiloque <code@...> wrote:
Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 






--




Re: Trying to figure out an elegant way to handle basicauth

Brian Gupta <brian.gupta@...>
 

Cool and thank you. Is there a more fleshed out example of this somewhere?

Thanks,
Brian


On Sat, Oct 8, 2011 at 5:02 AM, Archiloque <code@...> wrote:
Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 






--



Re: Trying to figure out an elegant way to handle basicauth

Archiloque <code@...>
 

Hi,

not directly with RestClient.get but
Request.execute(:method => :get, :url => url, :login => login, :password => password, :headers => { :accept => :json, :content_type => :json })
should do the trick

A.

Le 7 oct. 2011 à 08:19, Brian Gupta a écrit :

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 




Trying to figure out an elegant way to handle basicauth

Brian Gupta <brian.gupta@...>
 

In the main body of my code I parse args and set class variables for username and password, as well as set the basic url via an instance that I pass to rest_client. I got basicauth working by brute force disassembling and reassembling the url. I was hoping there was a more elegant way to do this. Any thoughts?

my_url = "https://host.domain.com/"

def get_collection(my_url,path)

  my_uri = URI.parse(my_url)
  my_url2 = my_uri.scheme + "://" + @@my_user + ":" + @@my_pass + "@" + my_uri.host + "/" + path.to_s

  response = RestClient.get my_url2,
                            { :accept => :json, :content_type => :json }
  results = JSON.parse(response.to_str)
end

Thanks,
Brian
-- 



Re: rest-client and RPC

Archiloque <code@...>
 

Hi,

I think so, you probably just need to add a small wrapper to automate the serialization/deserialization

A.

Le 21 sept. 2011 à 01:42, anderson leite <andersonlfl@...> a écrit :

Is it possible to make JSON-RPC calls using Rest-Client ?


rest-client and RPC

anderson leite <andersonlfl@...>
 

Is it possible to make JSON-RPC calls using Rest-Client ?


Re: Welcome to rest.client list.

Emiliano Heyns <Emiliano.Heyns@...>
 

OK, same problem, I've contacted their support. Thanks!


On Mon, Aug 15, 2011 at 21:18, Archiloque <code@...> wrote:
Sorry I was unclear, just add multipart as a parameter in the request:

project = 'redmine-backlogs'
r = RestClient.post("#{root}/update-file",
  :key => key,
  :json => 'json',
  :files => {:"Redmine Backlogs.yml" => File.new('/home/hnse/redmine/redmine_backlogs/config/locales/en.yml')},
  :multipart => true # that's it
  )

or else you can pull the latest version of the code from the repo on the "next" branch, I've improved the file detection this afternoon

A.

Le 15 août 2011 à 19:52, Emiliano Heyns a écrit :

I'll go check. To be sure, the code below ought to do the trick?

project = 'redmine-backlogs'
r = RestClient.post("#{root}/update-file",
  :key => key,
  :json => 'json',
  :files => {:"Redmine Backlogs.yml" => File.new('/home/hnse/redmine/redmine_backlogs/config/locales/en.yml')},
  :params => {:multipart => true}
  )

Thanks,
Emile

On Mon, Aug 15, 2011 at 19:37, Archiloque <code@...> wrote:
Hi,

no idea: when trying locally the log seems ok, could you try to check with their support to see if they see anything, or perhaps anybody else on the list has an idea ?

A.


Le 15 août 2011 à 17:34, Emiliano Heyns a écrit :

Same thing, "Bad Request".

On Mon, Aug 15, 2011 at 09:53, Archiloque <code@...> wrote:
Hi,

RestClient doesn't detect files in arrays (I'll fix it), could you try adding :multipart => true to the params ?

A.

Le 15 août 2011 à 00:50, Emiliano Heyns a écrit :

I'm trying to upload a file to the crowdin translation site. The following curl call does the job:


I'm using the following with rest_client, but I'm getting a "bad request" error:

  :key => 'blablabla',
  :files => {:"Redmine Backlogs.yml" => File.new('en.yml')}
  )

what is the correct way to send files[] type params?







Re: Welcome to rest.client list.

Archiloque <code@...>
 

Sorry I was unclear, just add multipart as a parameter in the request:

project = 'redmine-backlogs'
r = RestClient.post("#{root}/update-file",
  :key => key,
  :json => 'json',
  :files => {:"Redmine Backlogs.yml" => File.new('/home/hnse/redmine/redmine_backlogs/config/locales/en.yml')},
  :multipart => true # that's it
  )

or else you can pull the latest version of the code from the repo on the "next" branch, I've improved the file detection this afternoon

A.

Le 15 août 2011 à 19:52, Emiliano Heyns a écrit :

I'll go check. To be sure, the code below ought to do the trick?

project = 'redmine-backlogs'
r = RestClient.post("#{root}/update-file",
  :key => key,
  :json => 'json',
  :files => {:"Redmine Backlogs.yml" => File.new('/home/hnse/redmine/redmine_backlogs/config/locales/en.yml')},
  :params => {:multipart => true}
  )

Thanks,
Emile

On Mon, Aug 15, 2011 at 19:37, Archiloque <code@...> wrote:
Hi,

no idea: when trying locally the log seems ok, could you try to check with their support to see if they see anything, or perhaps anybody else on the list has an idea ?

A.


Le 15 août 2011 à 17:34, Emiliano Heyns a écrit :

Same thing, "Bad Request".

On Mon, Aug 15, 2011 at 09:53, Archiloque <code@...> wrote:
Hi,

RestClient doesn't detect files in arrays (I'll fix it), could you try adding :multipart => true to the params ?

A.

Le 15 août 2011 à 00:50, Emiliano Heyns a écrit :

I'm trying to upload a file to the crowdin translation site. The following curl call does the job:


I'm using the following with rest_client, but I'm getting a "bad request" error:

  :key => 'blablabla',
  :files => {:"Redmine Backlogs.yml" => File.new('en.yml')}
  )

what is the correct way to send files[] type params?






Re: Welcome to rest.client list.

Emiliano Heyns <Emiliano.Heyns@...>
 

I'll go check. To be sure, the code below ought to do the trick?

project = 'redmine-backlogs'
r = RestClient.post("#{root}/update-file",
  :key => key,
  :json => 'json',
  :files => {:"Redmine Backlogs.yml" => File.new('/home/hnse/redmine/redmine_backlogs/config/locales/en.yml')},
  :params => {:multipart => true}
  )

Thanks,
Emile


On Mon, Aug 15, 2011 at 19:37, Archiloque <code@...> wrote:
Hi,

no idea: when trying locally the log seems ok, could you try to check with their support to see if they see anything, or perhaps anybody else on the list has an idea ?

A.


Le 15 août 2011 à 17:34, Emiliano Heyns a écrit :

Same thing, "Bad Request".

On Mon, Aug 15, 2011 at 09:53, Archiloque <code@...> wrote:
Hi,

RestClient doesn't detect files in arrays (I'll fix it), could you try adding :multipart => true to the params ?

A.

Le 15 août 2011 à 00:50, Emiliano Heyns a écrit :

I'm trying to upload a file to the crowdin translation site. The following curl call does the job:


I'm using the following with rest_client, but I'm getting a "bad request" error:

  :key => 'blablabla',
  :files => {:"Redmine Backlogs.yml" => File.new('en.yml')}
  )

what is the correct way to send files[] type params?





Re: Welcome to rest.client list.

Archiloque <code@...>
 

Hi,

no idea: when trying locally the log seems ok, could you try to check with their support to see if they see anything, or perhaps anybody else on the list has an idea ?

A.


Le 15 août 2011 à 17:34, Emiliano Heyns a écrit :

Same thing, "Bad Request".

On Mon, Aug 15, 2011 at 09:53, Archiloque <code@...> wrote:
Hi,

RestClient doesn't detect files in arrays (I'll fix it), could you try adding :multipart => true to the params ?

A.

Le 15 août 2011 à 00:50, Emiliano Heyns a écrit :

I'm trying to upload a file to the crowdin translation site. The following curl call does the job:


I'm using the following with rest_client, but I'm getting a "bad request" error:

  :key => 'blablabla',
  :files => {:"Redmine Backlogs.yml" => File.new('en.yml')}
  )

what is the correct way to send files[] type params?