Postman throwing 400 Bad request for multipart/form-data image upload with jersey 2.0
Asked Answered
C

2

3

REQUEST :

URL: http://localhost:8080/RESTfulExample/rest/file/upload METHOD : POST

HEADER: Content-Type : multipart/form-data

RESPONSE :

HTTP Status 400 - Bad Request

The same code is working with html forms but in postman it's throwing 400 BAD REQUEST, I looked up on google for solution and found that boundary is missing, How to resolve it ? As I have to recieve files from multiple clients like mobile application and web clients via Jquery and rest client.

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {

        try {
            String uploadedFileLocation = "/home/nash/" + fileDetail.getFileName();

            // save it
            writeToFile(uploadedInputStream, uploadedFileLocation);

            String output = "File uploaded to : " + uploadedFileLocation;
            System.out.println("File uploaded..........");

            return Response.status(200).entity(output).build();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception " + e);
            return null;
        }

    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}
Chiffchaff answered 8/9, 2016 at 6:23 Comment(0)
S
13

Please follow these steps:

  1. Add jersey-multipart dependency.

  2. In Your Application Class (or in web.xml) enable MultipartFeature.class.

  3. DO NOT Add Content-Type header in your postman request.

For me the above steps worked. Do let me know if that helped you or not.

Spillway answered 12/12, 2016 at 15:9 Comment(5)
Thank you very much for the response and yes it worked for me .Chiffchaff
DO NOT Add Conent-Type misteriously worked. Why that?Clubby
I think multipart/form-data should work. Don't know haven't tried it yet.Chiffchaff
This worked for me too. I had to remove Content-Type which I had set to multipart/form-data. This does not make sense but at least it works!Airwoman
Yes. I suspected a few settings/configuration might be wrong with my Postman. So I just tried calling the service from Rest Client. It worked fine. So yes, some times, while changing some postman proxy settings or ssl/certificate settings, you might have accidentally turned some security feature on/off - which might be blocking the multipart request particularly.Enchanting
M
0

I had the same issue, make sure that you didn't modify your header, especially the content-type, and the content-length

Mayes answered 19/8, 2024 at 19:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.