Image Upload Code Sample - Java with Jersey

Image Upload Code Sample - Java with Jersey

import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;

public class ImageUploadSample {
    public static void main(String[] args) throws IOException
    {
        final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();

        final FileDataBodyPart filePart = new FileDataBodyPart("image", new File("{file_path"));
        FormDataMultiPart multipart = new FormDataMultiPart();

        multipart.bodyPart(filePart);

        final WebTarget target = client
.target("https://<<domain_prefix>>.retail.lightspeed.app/api/2.0/products/{product_id}/actions/image_upload");

        final Response response = target.request()
                .header("Authorization", "Bearer {token}")
                .post(Entity.entity(multipart, multipart.getMediaType()));

        System.out.println(response);

        multipart.close();
    }
}

Dependencies (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vendhq</groupId>
    <artifactId>image-upload-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.26-b03</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>

</project>