0

I've been trying to send a file (image) using Java and Springboot. Recently I've discovered that there is a code section in the Postman which can generate Java code, and thought it can be used for sending file (via post method), however I'm getting unsuccessful message, instead of successful, tested on the Postman with same parameters.
Here is how response looks like on postman:

{
    "status": "success",
    "message": "Media uploaded successfully.",
    "data": {
        "mediaId": "429f614c-e6d4-41a2-87fe-1209d7f25c6b"
    }
}

And code for that is following (there are two codes, one with Java-OkHttp, and the other with Java-Unirest, for some reason both are not working..below is the code with Unirest):

Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.post("https://waapi.pepipost.com/api/v2/media/upload/")
  .header("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuZXRjb3Jlc2FsZXNleHAiLCJleHAiOjI0MjUxMDI1MjZ9.ljC4Tvgz031i6DsKr2ILgCJsc9C_hxdo2Kw8iZp9tsVcCaKbIOXaFoXmpU7Yo7ob4P6fBtNtdNBQv_NSMq_Q8w")
  .header("Content-Type", "image/jpeg")
  .field("file", new File("/C:/Users/Slomil/Desktop/WABot/menu.jpg"))
  .asString();

When running that, I encounter the message

{"status":"failure","error":{"code":"8006","message":"Parameter file is required. Please upload a valid file."}}

for which I thought I shouldn't get..because there is a file as a field, I suppose? I'm not sure what is meant by validity..since i can open the file, it is regular jpg image with size 82.3 kb.. So far only know that the response message is opposite than on Postman:)
Does anyone know why is that? Is that generated code reliable to use (instead of using eg. CloseableHttpClient and similar)?
Thank you very much..

slomil
  • 193
  • 1
  • 4
  • 12

1 Answers1

0

I solved the issue, if anyone encounters this.. it isn't necessary to specify Content-Type, so when I left it out, it worked.:) This post helped me The request was rejected because no multipart boundary was found in springboot and the first answer..

slomil
  • 193
  • 1
  • 4
  • 12