Post ActionDispatch::Http::UploadedFile object


David Piegza <piegza@...>
 

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David


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

Can you pass an open File handle?

headers = Hash.new
File.open("whatever.txt", "r") do |io|
RestClient.post("url", {:file => io}, headers)
end

Bye,
François

Le vendredi 5 août 2011 à 07:43, David Piegza a écrit :

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David


David Piegza <piegza@...>
 

The data comes from an HTML-Form (File upload) and is wrapped in the UploadedFile object, so I can not put the data to a file handle, I guess..

I have tried this workaround:

headers = {
:content_type => 'image/jpeg'
}
RestClient.post('http://example.com/resource', { :file => uploaded_file.tempfile }, headers)

The image data in uploaded_file.tempfile is sent correctly, but on the receiver the content-type is set to "text/plain". I'm not able to change it to "image/jpeg"... it would solve the problem if I could set the content-type.

Thanks,
David

On 5 Aug 2011, at 15:42, François Beausoleil wrote:

Can you pass an open File handle?

headers = Hash.new
File.open("whatever.txt", "r") do |io|
RestClient.post("url", {:file => io}, headers)
end

Bye,
François

Le vendredi 5 août 2011 à 07:43, David Piegza a écrit :

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David


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

Hello David,

Remember you're sending a form. The request's Content-Type MUST multipart/form-data, because you have a File handle. The part's Content-Type can be set, according to the spec. Looking at the implementation (https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L15 and https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L182), this should all happen automatically. RestClient's Payload / Multipart class should determine the content-type automatically.

On the other hand, if you're trying to stream the File directly, then it's a whole different ballgame: you may want to do it this way instead:

RestClient.post("url", File.read("whatever.jpg"), :content_type => "image/jpeg")

Notice the payload is a String, not an IO, not a Hash. Thus, server-side, you would need to get access to the raw Request's stream, rather than accessing form parameters.

Hope that helps!
François

Le vendredi 5 août 2011 à 10:20, David Piegza a écrit :


The data comes from an HTML-Form (File upload) and is wrapped in the UploadedFile object, so I can not put the data to a file handle, I guess..

I have tried this workaround:

headers = {
:content_type => 'image/jpeg'
}
RestClient.post('http://example.com/resource', { :file => uploaded_file.tempfile }, headers)

The image data in uploaded_file.tempfile is sent correctly, but on the receiver the content-type is set to "text/plain". I'm not able to change it to "image/jpeg"... it would solve the problem if I could set the content-type.

Thanks,
David


On 5 Aug 2011, at 15:42, François Beausoleil wrote:

Can you pass an open File handle?

headers = Hash.new
File.open("whatever.txt", "r") do |io|
RestClient.post("url", {:file => io}, headers)
end

Bye,
François

Le vendredi 5 août 2011 à 07:43, David Piegza a écrit :

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David


David Piegza <piegza@...>
 

Hey François,

I still have a problem with this. What I'm trying to do is the following:

- Upload a file from an HTML-Form (multipart/form-data is set)
- In the Rails-Controller I get the file in an ActionDispatch UploadedFile object

- Now, I have to post this file to a third-party API, so I'm trying to post the file (the UploadedFile object) to the API but this does not work properly.
- What does work is, to read the contents of the UploadedFile object, save it to a temp file, and then read the file and post it with RestClient. Here is the code:

# Get UploadedFile Object (from HTML-Form)
file = params[:profile][:upload]

filename = file.original_filename
directory = "#{Rails.root}/tmp/"

path = File.join(directory, filename)
# write the file
File.open(path, "wb") { |f| f.write(file.read) }

headers = {}
RestClient.post('http://example.com/resource', {:file => File.open(path, "rb")}, headers)

With that code, the content-type is set to 'image/jpeg' on the receiver, which is what I want. When I use this code instead:

RestClient.post('http://example.com/resource', {:file => file.tempfile}, headers)

The content-type is set to 'text/plain'.

The code above looks a bit weird to me, I wonder if I could just pass the UploadedFile object to RestClient and post it to the API, but it should have the appropriate content-type. Let me know if you have any hints.

Thanks for your support!

David

On 5 Aug 2011, at 17:17, François Beausoleil wrote:

Hello David,

Remember you're sending a form. The request's Content-Type MUST multipart/form-data, because you have a File handle. The part's Content-Type can be set, according to the spec. Looking at the implementation (https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L15 and https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L182), this should all happen automatically. RestClient's Payload / Multipart class should determine the content-type automatically.

On the other hand, if you're trying to stream the File directly, then it's a whole different ballgame: you may want to do it this way instead:

RestClient.post("url", File.read("whatever.jpg"), :content_type => "image/jpeg")

Notice the payload is a String, not an IO, not a Hash. Thus, server-side, you would need to get access to the raw Request's stream, rather than accessing form parameters.

Hope that helps!
François

Le vendredi 5 août 2011 à 10:20, David Piegza a écrit :


The data comes from an HTML-Form (File upload) and is wrapped in the UploadedFile object, so I can not put the data to a file handle, I guess..

I have tried this workaround:

headers = {
:content_type => 'image/jpeg'
}
RestClient.post('http://example.com/resource', { :file => uploaded_file.tempfile }, headers)

The image data in uploaded_file.tempfile is sent correctly, but on the receiver the content-type is set to "text/plain". I'm not able to change it to "image/jpeg"... it would solve the problem if I could set the content-type.

Thanks,
David


On 5 Aug 2011, at 15:42, François Beausoleil wrote:

Can you pass an open File handle?

headers = Hash.new
File.open("whatever.txt", "r") do |io|
RestClient.post("url", {:file => io}, headers)
end

Bye,
François

Le vendredi 5 août 2011 à 07:43, David Piegza a écrit :

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David


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

Sorry, I don't have anymore tips. Maybe someone else can give further hints?

I'd try to send the UploadedFile to RestClient, maybe it can do the right thing directly.

Bye,
François

Le vendredi 5 août 2011 à 13:24, David Piegza a écrit :

Hey François,

I still have a problem with this. What I'm trying to do is the following:

- Upload a file from an HTML-Form (multipart/form-data is set)
- In the Rails-Controller I get the file in an ActionDispatch UploadedFile object

- Now, I have to post this file to a third-party API, so I'm trying to post the file (the UploadedFile object) to the API but this does not work properly.
- What does work is, to read the contents of the UploadedFile object, save it to a temp file, and then read the file and post it with RestClient. Here is the code:

# Get UploadedFile Object (from HTML-Form)
file = params[:profile][:upload]

filename = file.original_filename
directory = "#{Rails.root}/tmp/"

path = File.join(directory, filename)
# write the file
File.open(path, "wb") { |f| f.write(file.read) }

headers = {}
RestClient.post('http://example.com/resource', {:file => File.open(path, "rb")}, headers)

With that code, the content-type is set to 'image/jpeg' on the receiver, which is what I want. When I use this code instead:

RestClient.post('http://example.com/resource', {:file => file.tempfile}, headers)

The content-type is set to 'text/plain'.

The code above looks a bit weird to me, I wonder if I could just pass the UploadedFile object to RestClient and post it to the API, but it should have the appropriate content-type. Let me know if you have any hints.

Thanks for your support!

David




On 5 Aug 2011, at 17:17, François Beausoleil wrote:

Hello David,

Remember you're sending a form. The request's Content-Type MUST multipart/form-data, because you have a File handle. The part's Content-Type can be set, according to the spec. Looking at the implementation (https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L15 and https://github.com/archiloque/rest-client/blob/master/lib/restclient/payload.rb#L182), this should all happen automatically. RestClient's Payload / Multipart class should determine the content-type automatically.

On the other hand, if you're trying to stream the File directly, then it's a whole different ballgame: you may want to do it this way instead:

RestClient.post("url", File.read("whatever.jpg"), :content_type => "image/jpeg")

Notice the payload is a String, not an IO, not a Hash. Thus, server-side, you would need to get access to the raw Request's stream, rather than accessing form parameters.

Hope that helps!
François

Le vendredi 5 août 2011 à 10:20, David Piegza a écrit :


The data comes from an HTML-Form (File upload) and is wrapped in the UploadedFile object, so I can not put the data to a file handle, I guess..

I have tried this workaround:

headers = {
:content_type => 'image/jpeg'
}
RestClient.post('http://example.com/resource', { :file => uploaded_file.tempfile }, headers)

The image data in uploaded_file.tempfile is sent correctly, but on the receiver the content-type is set to "text/plain". I'm not able to change it to "image/jpeg"... it would solve the problem if I could set the content-type.

Thanks,
David


On 5 Aug 2011, at 15:42, François Beausoleil wrote:

Can you pass an open File handle?

headers = Hash.new
File.open("whatever.txt", "r") do |io|
RestClient.post("url", {:file => io}, headers)
end

Bye,
François

Le vendredi 5 août 2011 à 07:43, David Piegza a écrit :

Hi,

is there a way to post an ActionDispatch::Http::UploadedFile object with RestClient? It does not work with:

RestClient.post('http://example.com/resource', {:file => uploaded_file}, headers)

where uploaded_file is an ActionDispatch::Http::UploadedFile object. All I get is

Parameters: {"file"=>"#<ActionDispatch::Http::UploadedFile:0x007fa54d63ad40>"}, which is a string representation of the class and not the object itself.

Any hints?

Thanks!
David