0
votes

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=#>
1

1 Answers

2
votes

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