0

I want to send an HTTP post with one form parameter as File and Another one which sends a list of numbers.

Request:

curl -X POST \ http://127.0.0.1:5001/verify \ -H 'cache-control: no-cache' \ -H 'content-type: multipart/form-data; boundary=---- -F 'items=["Apple", "Orange"]' \ -F 'photo=@/home/.../img.jpg'

MarkZ
  • 29
  • 9

1 Answers1

0
Client client = ClientBuilder.newBuilder()
        .register(MultiPartFeature.class)
        .build();

MultiPart multiPart = new FormDataMultiPart()
        .field("items", "[\"Apple\", \"Orang\"]", MediaType.APPLICATION_JSON_TYPE)
        .bodyPart(new FileDataBodyPart("photo", new File("img.png"));

Response response = client
        .target(url)
        .request()
        .header(HttpHeaders.CACHE_CONTROL, "no-cache")
        .post(Entity.entity(multiPart), multiPart.getMediaType());

For this to work, you need to add the dependency

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey2.version}</version>
</dependency>

You tagged , so I assume you are already using/have Jersey client. If you don't then add

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey2.version}</version>
</dependency>

See also

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720