1

I am trying to upload a file with content type Multipart/form-data in Java. I have received a file in the InputStream format, so I need to upload this InputStream content into the target place.

I successfully uploaded this file using Postman with Multipart/form data parameters. But I am unable to do it in Java. I tried with OK HTTP. I don't know how to put a file content in form data. If I use below code as it is, file uploaded but with empty data.

I copied this code from Postman:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Bot.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
      .url("https://test.atlassian.net/rest/api/2/issue/JIR-1/attachments")
      .post(body)
      .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
      .addHeader("authorization", "Basic abcdefg")
      .addHeader("x-atlassian-token", "nocheck")
      .addHeader("cache-control", "no-cache")
      .addHeader("postman-token", "a039d7b4-6d95-98a7-d4ac-606606b9c121")
      .build();

Response response = client.newCall(request).execute();
dur
  • 15,689
  • 25
  • 79
  • 125
RakeshSharma227
  • 361
  • 1
  • 5
  • 15

0 Answers0