Error message: 'image' is a required property

I call the image editing API via HttpClient (Java). I get the error message: ‘image’ is a required property although the image is provided. What am I doing wrong here?

File file = new File(“mypic.png”);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(“link-to-openai-api”);

  MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  builder.addBinaryBody("image", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
  builder.addTextBody("prompt", "surrounded by lions");
  httpPost.setHeader("Content-Type", "multipart/form-data");
  httpPost.setHeader("Authorization", "Bearer sk-xxxxxxx");
  CloseableHttpResponse res = httpclient.execute(httpPost);

It’s unbelievable how many people got this wrong.

You need to set the boundary parameter in your content type like this:

‘Content-Type: multipart/form-data; boundary=${boundary}’