IOException: Missing content for multipart request










1















I am trying to send an image via a POST request from an Android app to a Heroku webserver. In the webserver, I want to retrieve the image from the request, modify and then send the modified image back as a response.



However, my current code returns an an IOException in the webserver saying




java.io.IOException: Missing content for multipart request
org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)




I checked and the userImageFile does exist in the Android app at least.



This is my code in the Android app (using OkHttp).



 //Creating file with the bitmap gotten from the user
String path = this.getFilesDir().getAbsolutePath();
File userImageFile = new File(path + "/image.png");
userImageFile.createNewFile();
FileOutputStream fop = new FileOutputStream(userImageFile, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fop);
fop.flush();
fop.close();

OkHttpClient okHttpClient = new OkHttpClient();
String url = "https://my-heroku-app-url-here.com/imageConvert";
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", userImageFile.getName(),
RequestBody.create(MediaType.parse("image/png"), userImageFile))
.build();

Request request = new Request.Builder()
.url(url)
.post(body)
.build();

Response response = okHttpClient.newCall(request).execute();


And this is my code for the Heroku webserver (using the Spark framework).



post("/imageConvert", (request, response) -> 
byte body = request.bodyAsBytes();
request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
BufferedImage returnImage = null;
try (InputStream is = request.raw().getPart("image").getInputStream())
BufferedImage userImage = ImageIO.read(is);
returnImage = getDistortedImage(userImage);
catch (IOException ex)
return "There has been an IO Exception: n" + ex.getMessage();

if(returnImage!= null)
ImageIO.write(returnImage, "png", response.raw().getOutputStream());
return response.raw();

return "There was an unknown mistake";
);









share|improve this question




























    1















    I am trying to send an image via a POST request from an Android app to a Heroku webserver. In the webserver, I want to retrieve the image from the request, modify and then send the modified image back as a response.



    However, my current code returns an an IOException in the webserver saying




    java.io.IOException: Missing content for multipart request
    org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)




    I checked and the userImageFile does exist in the Android app at least.



    This is my code in the Android app (using OkHttp).



     //Creating file with the bitmap gotten from the user
    String path = this.getFilesDir().getAbsolutePath();
    File userImageFile = new File(path + "/image.png");
    userImageFile.createNewFile();
    FileOutputStream fop = new FileOutputStream(userImageFile, false);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fop);
    fop.flush();
    fop.close();

    OkHttpClient okHttpClient = new OkHttpClient();
    String url = "https://my-heroku-app-url-here.com/imageConvert";
    RequestBody body = new MultipartBody.Builder()
    .setType(MultipartBody.FORM)
    .addFormDataPart("image", userImageFile.getName(),
    RequestBody.create(MediaType.parse("image/png"), userImageFile))
    .build();

    Request request = new Request.Builder()
    .url(url)
    .post(body)
    .build();

    Response response = okHttpClient.newCall(request).execute();


    And this is my code for the Heroku webserver (using the Spark framework).



    post("/imageConvert", (request, response) -> 
    byte body = request.bodyAsBytes();
    request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
    BufferedImage returnImage = null;
    try (InputStream is = request.raw().getPart("image").getInputStream())
    BufferedImage userImage = ImageIO.read(is);
    returnImage = getDistortedImage(userImage);
    catch (IOException ex)
    return "There has been an IO Exception: n" + ex.getMessage();

    if(returnImage!= null)
    ImageIO.write(returnImage, "png", response.raw().getOutputStream());
    return response.raw();

    return "There was an unknown mistake";
    );









    share|improve this question


























      1












      1








      1


      2






      I am trying to send an image via a POST request from an Android app to a Heroku webserver. In the webserver, I want to retrieve the image from the request, modify and then send the modified image back as a response.



      However, my current code returns an an IOException in the webserver saying




      java.io.IOException: Missing content for multipart request
      org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)




      I checked and the userImageFile does exist in the Android app at least.



      This is my code in the Android app (using OkHttp).



       //Creating file with the bitmap gotten from the user
      String path = this.getFilesDir().getAbsolutePath();
      File userImageFile = new File(path + "/image.png");
      userImageFile.createNewFile();
      FileOutputStream fop = new FileOutputStream(userImageFile, false);
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fop);
      fop.flush();
      fop.close();

      OkHttpClient okHttpClient = new OkHttpClient();
      String url = "https://my-heroku-app-url-here.com/imageConvert";
      RequestBody body = new MultipartBody.Builder()
      .setType(MultipartBody.FORM)
      .addFormDataPart("image", userImageFile.getName(),
      RequestBody.create(MediaType.parse("image/png"), userImageFile))
      .build();

      Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();

      Response response = okHttpClient.newCall(request).execute();


      And this is my code for the Heroku webserver (using the Spark framework).



      post("/imageConvert", (request, response) -> 
      byte body = request.bodyAsBytes();
      request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
      BufferedImage returnImage = null;
      try (InputStream is = request.raw().getPart("image").getInputStream())
      BufferedImage userImage = ImageIO.read(is);
      returnImage = getDistortedImage(userImage);
      catch (IOException ex)
      return "There has been an IO Exception: n" + ex.getMessage();

      if(returnImage!= null)
      ImageIO.write(returnImage, "png", response.raw().getOutputStream());
      return response.raw();

      return "There was an unknown mistake";
      );









      share|improve this question
















      I am trying to send an image via a POST request from an Android app to a Heroku webserver. In the webserver, I want to retrieve the image from the request, modify and then send the modified image back as a response.



      However, my current code returns an an IOException in the webserver saying




      java.io.IOException: Missing content for multipart request
      org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)




      I checked and the userImageFile does exist in the Android app at least.



      This is my code in the Android app (using OkHttp).



       //Creating file with the bitmap gotten from the user
      String path = this.getFilesDir().getAbsolutePath();
      File userImageFile = new File(path + "/image.png");
      userImageFile.createNewFile();
      FileOutputStream fop = new FileOutputStream(userImageFile, false);
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fop);
      fop.flush();
      fop.close();

      OkHttpClient okHttpClient = new OkHttpClient();
      String url = "https://my-heroku-app-url-here.com/imageConvert";
      RequestBody body = new MultipartBody.Builder()
      .setType(MultipartBody.FORM)
      .addFormDataPart("image", userImageFile.getName(),
      RequestBody.create(MediaType.parse("image/png"), userImageFile))
      .build();

      Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();

      Response response = okHttpClient.newCall(request).execute();


      And this is my code for the Heroku webserver (using the Spark framework).



      post("/imageConvert", (request, response) -> 
      byte body = request.bodyAsBytes();
      request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
      BufferedImage returnImage = null;
      try (InputStream is = request.raw().getPart("image").getInputStream())
      BufferedImage userImage = ImageIO.read(is);
      returnImage = getDistortedImage(userImage);
      catch (IOException ex)
      return "There has been an IO Exception: n" + ex.getMessage();

      if(returnImage!= null)
      ImageIO.write(returnImage, "png", response.raw().getOutputStream());
      return response.raw();

      return "There was an unknown mistake";
      );






      java android apache-spark heroku okhttp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 6:02









      Ankur Chrungoo

      60839




      60839










      asked Nov 15 '18 at 17:37









      raphanusraphanus

      82




      82






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I think that you are consuming the request body twice. If the request contents are consumed once, then you can no longer consume the bytes / stream again just like that, without some explicit resetting mechanism.



          You should remove the first statement, so that the second statement is able to consume the input stream.



          1. byte body = request.bodyAsBytes(); // Remove this

          2. try (InputStream is = request.raw().getPart("image").getInputStream())





          share|improve this answer

























          • Yup, that works. Can't believe I missed that. Thanks!

            – raphanus
            Nov 15 '18 at 20:31











          • Great! Glad to help!

            – Ankur Chrungoo
            Nov 16 '18 at 5:19










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325066%2fioexception-missing-content-for-multipart-request%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I think that you are consuming the request body twice. If the request contents are consumed once, then you can no longer consume the bytes / stream again just like that, without some explicit resetting mechanism.



          You should remove the first statement, so that the second statement is able to consume the input stream.



          1. byte body = request.bodyAsBytes(); // Remove this

          2. try (InputStream is = request.raw().getPart("image").getInputStream())





          share|improve this answer

























          • Yup, that works. Can't believe I missed that. Thanks!

            – raphanus
            Nov 15 '18 at 20:31











          • Great! Glad to help!

            – Ankur Chrungoo
            Nov 16 '18 at 5:19















          1














          I think that you are consuming the request body twice. If the request contents are consumed once, then you can no longer consume the bytes / stream again just like that, without some explicit resetting mechanism.



          You should remove the first statement, so that the second statement is able to consume the input stream.



          1. byte body = request.bodyAsBytes(); // Remove this

          2. try (InputStream is = request.raw().getPart("image").getInputStream())





          share|improve this answer

























          • Yup, that works. Can't believe I missed that. Thanks!

            – raphanus
            Nov 15 '18 at 20:31











          • Great! Glad to help!

            – Ankur Chrungoo
            Nov 16 '18 at 5:19













          1












          1








          1







          I think that you are consuming the request body twice. If the request contents are consumed once, then you can no longer consume the bytes / stream again just like that, without some explicit resetting mechanism.



          You should remove the first statement, so that the second statement is able to consume the input stream.



          1. byte body = request.bodyAsBytes(); // Remove this

          2. try (InputStream is = request.raw().getPart("image").getInputStream())





          share|improve this answer















          I think that you are consuming the request body twice. If the request contents are consumed once, then you can no longer consume the bytes / stream again just like that, without some explicit resetting mechanism.



          You should remove the first statement, so that the second statement is able to consume the input stream.



          1. byte body = request.bodyAsBytes(); // Remove this

          2. try (InputStream is = request.raw().getPart("image").getInputStream())






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 5:38

























          answered Nov 15 '18 at 18:05









          Ankur ChrungooAnkur Chrungoo

          60839




          60839












          • Yup, that works. Can't believe I missed that. Thanks!

            – raphanus
            Nov 15 '18 at 20:31











          • Great! Glad to help!

            – Ankur Chrungoo
            Nov 16 '18 at 5:19

















          • Yup, that works. Can't believe I missed that. Thanks!

            – raphanus
            Nov 15 '18 at 20:31











          • Great! Glad to help!

            – Ankur Chrungoo
            Nov 16 '18 at 5:19
















          Yup, that works. Can't believe I missed that. Thanks!

          – raphanus
          Nov 15 '18 at 20:31





          Yup, that works. Can't believe I missed that. Thanks!

          – raphanus
          Nov 15 '18 at 20:31













          Great! Glad to help!

          – Ankur Chrungoo
          Nov 16 '18 at 5:19





          Great! Glad to help!

          – Ankur Chrungoo
          Nov 16 '18 at 5:19



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325066%2fioexception-missing-content-for-multipart-request%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto