Class MultiPartRequestContent

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, Request.Content

    public class MultiPartRequestContent
    extends AbstractRequestContent
    implements java.io.Closeable

    A Request.Content for form uploads with the "multipart/form-data" content type.

    Example usage:

     MultiPartRequestContent multiPart = new MultiPartRequestContent();
     multiPart.addFieldPart("field", new StringRequestContent("foo"), null);
     multiPart.addFilePart("icon", "img.png", new PathRequestContent(Paths.get("/tmp/img.png")), null);
     multiPart.close();
     ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
             .method(HttpMethod.POST)
             .content(multiPart)
             .send();
     

    The above example would be the equivalent of submitting this form:

     <form method="POST" enctype="multipart/form-data"  accept-charset="UTF-8">
         <input type="text" name="field" value="foo" />
         <input type="file" name="icon" />
     </form>
     
    • Constructor Detail

      • MultiPartRequestContent

        public MultiPartRequestContent()
      • MultiPartRequestContent

        public MultiPartRequestContent​(java.lang.String boundary)
    • Method Detail

      • getLength

        public long getLength()
        Specified by:
        getLength in interface Request.Content
        Returns:
        the content length, if known, or -1 if the content length is unknown
      • fail

        public void fail​(java.lang.Throwable failure)
        Description copied from interface: Request.Content

        Fails this request content, possibly failing and discarding accumulated content that was not demanded.

        The failure may be notified to the consumer at a later time, when the consumer demands for content.

        Typical failure: the request being aborted by user code, or idle timeouts.

        Specified by:
        fail in interface Request.Content
        Parameters:
        failure - the reason of the failure
      • addFieldPart

        public void addFieldPart​(java.lang.String name,
                                 Request.Content content,
                                 HttpFields fields)

        Adds a field part with the given name as field name, and the given content as part content.

        The Content-Type of this part will be obtained from:

        Parameters:
        name - the part name
        content - the part content
        fields - the headers associated with this part
      • addFilePart

        public void addFilePart​(java.lang.String name,
                                java.lang.String fileName,
                                Request.Content content,
                                HttpFields fields)

        Adds a file part with the given name as field name, the given fileName as file name, and the given content as part content.

        The Content-Type of this part will be obtained from:

        Parameters:
        name - the part name
        fileName - the file name associated to this part
        content - the part content
        fields - the headers associated with this part
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable