Hi Norm,

I think this might be what you are looking for:

package norm1;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.FormBodyPartBuilder;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;

public class MultipartExample {

    public static void main(final String args[]) throws IOException {

        try(final CloseableHttpClient client =
HttpClientBuilder.create().build()) {

            final FormBodyPart part1 = FormBodyPartBuilder.create()
                    .setName("part1")
                    .addField("X-HELLO", "adam")
                    .addField("X-HELLO", "norm")
                    .setBody(new StringBody("<some-body/>",
ContentType.TEXT_XML))
                    .build();

            final FormBodyPart part2 = FormBodyPartBuilder.create()
                    .setName("part2")
                    .addField("X-BYE", "adam")
                    .addField("X-BYE", "norm")
                    .setBody(new StringBody("{\"some\": \"json\"}",
ContentType.APPLICATION_JSON))
                    .build();

            final HttpEntity entity = MultipartEntityBuilder.create()
                    .addPart(part1)
                    .addPart(part2)
                    .build();


            final HttpPost post = new HttpPost("http://localhost:8080";);
            post.setEntity(entity);

            client.execute(post);
        }
    }
}


On my system that produces a HTTP Request like:

POST / HTTP/1.1
Content-Length: 456
Content-Type: multipart/form-data; boundary=ZQwFLuoO6V1SFdqg2lI6DaVwlvKIZjj2Pb
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.8 (Java/1.8.0_202)
Accept-Encoding: gzip,deflate

--ZQwFLuoO6V1SFdqg2lI6DaVwlvKIZjj2Pb
X-HELLO: adam
X-HELLO: norm
Content-Disposition: form-data; name="part1"
Content-Type: text/xml; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

<some-body/>
--ZQwFLuoO6V1SFdqg2lI6DaVwlvKIZjj2Pb
X-BYE: adam
X-BYE: norm
Content-Disposition: form-data; name="part2"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{"some": "json"}
--ZQwFLuoO6V1SFdqg2lI6DaVwlvKIZjj2Pb--


Personally I find the naming of some of these things
inaccurate/misleading... i.e. the class `FormBodyPart`, and the method
`addField` (which appears to actually add a header). Regardless it
seems to "just about work".

Hope that helps?

Kind Regards. Adam.

On Sat, 1 Jun 2019 at 18:58, Norman Walsh <[email protected]> wrote:
>
> Hi all,
>
> Apologies if I’ve been down this road before on this list. The
> HttpClient 4.5 library has changed the way multipart works (from some
> earlier 4.x where I had it working).
>
> Near as I can tell from looking at the multipart examples in 4.5,
> they’re all geared towards file uploading. I’m not interested in file
> uploading, I want to construct a payload for a web service that’s
> expecting a multipart/mixed request: I want complete control over the
> parts, the headers associated with those parts, their content types,
> etc.
>
> I stumbled from HttpClient to MIME4J, prehaps on the advice of someone
> from this list. I got a little further that way, at least in as much
> as I now believe I’ve constructed a mime4j.message that is logically
> what I want to send.
>
> But I cannot see how to get from that back to an HttpEntity that I can
> set as the entity for an HttpClient request.
>
> Does anyone have an example of sending a fully general multipart/mixed
> example? Or is anyone familiar enough with both HttpClient and MIME4J
> to point me to an explanation of how to turn one of those into a
> request Entity?
>
> (Multipart is only one possibility so I’d really, really rather not
> have to have two entirely different code paths where I use HttpClient
> for some requests and use direct serialization of MIME4J payloads over
> a URLConnection for the other.)
>
> Help and advice most humbly solicited.
>
>                                         Be seeing you,
>                                           norm
>
> --
> Norman Walsh <[email protected]> | Why do writers write? Because it isn't
> http://nwalsh.com/            | there.--Thomas Berger



--
Adam Retter

eXist Core Developer
{ United Kingdom }
[email protected]

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to