UnsupportedOperationException Multipart form entity does not implement #getContent()

If I construct a new instance of MultipartEntity and display it's content length as below

 
(def bar (org.apache.http.entity.mime.MultipartEntity. ))
(print (.getContentLength bar))
42
nil
 

I shows the content length is 42 even it's a brand new instance. Then I try to show it's content with the following code

 
(org.apache.http.util.EntityUtils/toString bar)
 

I get this error:

 
UnsupportedOperationException Multipart form entity does not implement #getContent()  org.apache.http.entity.mime.MultipartEntity.getContent (MultipartEntity.java:178)
 

Since the source code is readily available, here is how it's implemented, look like this method is never supposed to be invoked.

 
    public InputStream getContent() throws IOException, UnsupportedOperationException {
        throw new UnsupportedOperationException(
                    "Multipart form entity does not implement #getContent()");
    }
 

If you get this error, it means somewhere in your code try to read out the content of a MultipartEntity instance.

The source code indicate the purpose of this class is to let you enclose multiple files in one entity and put it in a HTTP POST request and send them to server. Each file is called a part, but you can think of it as a key value paired map, each item in the map is a part or as the code shows a content body.

The correct way see the content in the MultipartEntity is to call writeTo method

 
(.writeTo bar java.lang.System.out)
 

The console display something like this:

 
--yd1TM1KE0RDoBDvQBzu9xQO-45zvUSRzDPZt--