Question

I am trying to upload data as multipart using RestClient like so:

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

The io argument is a StringIO that contains my file, so it's from memory instead of from the disk.

The server (Tomcat servlet) is unable to read the multipart data, giving an error:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

So I believe that RestClient is not sending it in multipart format? Anyone see the problem? I am assuming the problem is on the Ruby (client) side, but I can post my servlet (Spring) code if anyone thinks it might be a server-side problem.

I also wonder what RestClient would use for the uploaded filename, since there isn't an actual file... Can you have a multipart request without a filename?

Was it helpful?

Solution 2

After consulting with the author of the rest-client library (Archiloque), it seems that if this is possible, the API is not set up to handle it easily. Using the :multipart => true parameter will cause the IO to be treated like a file, and it looks for a non-nil #path on the IO, which for a StringIO is always nil.

If anyone needs this in the future, you'll need to consult with the library's mailing list (code@archiloque.net), as the author seems to think it is possible but perhaps not straightforward.

It CAN easily do streaming uploads from an IO as long as it's not multipart format, which is what I ended up settling for.

OTHER TIPS

You can do this, it simply requires subclassing StringIO and adding a non-nil path method to it:

class MailIO < StringIO
  def path
    'message'
  end
end

I've just checked this, and the Mailgun api is pretty down with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top