0

I have a RestClient that does the following call:

RestClient::Request.new(
    :method => "post",
    :url => "http://myservice.com/call.json",       
    :payload => {'document[data]' => File.new(e.image.path, 'rb')},
    :headers => { :accept => :json, :content_type => :json}
    ).execute

I've implemented a little test server that receives the rest call, since i'm not the author of the REST service. Below you find what the request actually is and what it should be. My question is: where do I specify the mime type of the file attached?

What it is:

#<ActionDispatch::Http::UploadedFile:0x0... 
@original_filename="x.pdf", @content_type="text/plain", 
@headers="Content-Disposition: form-data; name=\"document[data]\"; 
filename=\"x.pdf\"\r\nContent-Type: text/plain\r\n", 
@tempfile=#>

what it should be instead:

#<ActionDispatch::Http::UploadedFile:0x0...
@original_filename="x.pdf", @content_type="application/pdf", 
@headers="Content-Disposition: form-data; name=\"document[data]\"; 
filename=\"x.pdf\"\r\nContent-Type: application/pdf\r\n", 
@tempfile=#>
AgostinoX
  • 7,477
  • 20
  • 77
  • 137

1 Answers1

2

Strange, RestClient should be able to get the mime type based on the file extension.

Anyway, if I remember correctly there is no way to pass along a mime type for a multipart segment based on a file but there is code in the Payload class that looks for a content_type. This may have been fixed but this hack should work until this issue is resolved.

module MyModule
   def content_type
     "application/pdf"
   end
end

f = File.new(e.image.path, 'rb')}
f.extend(MyModule)

RestClient::Request.new(
   :method => "post",
   :url => "http://myservice.com/call.json",       
   :payload => {'document[data]' => f},
   :headers => { :accept => :json, :content_type => :json}
   ).execute
Pafjo
  • 4,979
  • 3
  • 23
  • 31
  • this answer helped me so much: upvoting is not enough!!! on the other hand i've to check for a non-tricky solution. i think i start from your solution, checking in where RestClient calls your content_type method – AgostinoX Aug 08 '14 at 19:31