Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

How to upload a file and JSON data in Postman? Example Tutorial

How to upload a file and JSON data in Postman is a common question when you are dealing with REST APIs. In order to do this, you need to send through POST request. So in this article, we are going to explain this using different examples where you can upload a single file in different ways. upload a list of files, upload as an object or upload a list of objects containing images from postman.

So before moving to the following different aspects of uploading a single file in different ways, let's assume that in your postman, you are using header named "Content-Type" with the value of "multipart/form-data".


How to upload a file and JSON data in Postman? Example Tutorial






                            The figure showing the content-type multipart/form-data in postman.

Spring boot multipart file upload example

So in this example, we are going to create a REST API which consumes multipart request data. But in order to do this, your application must have input parameter which gets the multipartFile and this will auto get by the spring. Following is an example code, which implemented the scenario.


@Slf4j
@Controller
public class {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@PostMapping(value = "/examplefileupload",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
public ResponseEntity<String> uploadFile(MultipartFile file) {
LOGGER.info("Request contains, File: " + file.getOriginalFilename());
return ResponseEntity.ok("Success");
}
}
Now from the postman, send the details as follows. 

Spring boot multipart file upload with @RequestParam 


This method is same as the previous discussed example, but only difference in here is usage of
@RequestParam annotation for input argument.

@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @PostMapping(value = "/examplefileupload/withparameter",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithParameter(@RequestParam MultipartFile file) {
    log.info("File upload with parameter:" + file.getOriginalFilename());
    return ResponseEntity.ok("Success");
    }
}
So in here also working with above postmain request.

Spring boot multipart file upload with @RequestPart

So in here, there use a @RequestPart annotation as the input argument. Others are remain
unchanged when compare to above examples.

@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @PostMapping(value = "/examplefileupload/withrequestpart",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithRequestPart(@RequestPart MultipartFile file) {
    log.info("Request contains, File: " + file.getOriginalFilename());
    return ResponseEntity.ok("Success");
    }
}
So again you don't need to change the way how postman request happens and it is exactly as above.


Spring boot multipart file upload with @RequestBody

So in this example, the @RequestBody is used to input the file.

@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @PostMapping(value = "/examplefileupload/withrequestbody",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithRequestBody(@RequestBody MultipartFile file) {
    log.info("Request file with request body" + file.getOriginalFilename());
    return ResponseEntity.ok("Success");
    }
}
There is no change with the postman request and you can rely on the above postman request.
    

Spring boot multipart file upload as an array

So in here, we upload the files as an array, In the method input argument, you need to specify an array of MutliPartFile type. 


@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @PostMapping(value = "/examplefileupload/withrequestAsArray",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithArray(MultipartFile[] files) {
    log.info("Request files as an array" + files.length);
    return ResponseEntity.ok("Success");
    }
}

So in postman, we need to upload multiple files and the files should have same name as the
input argument in rest api. Spring is responsible to convert those files into that inserted with
same name. So given below of how we post request using the Postman.




Spring boot multipart file upload as a list

So in here, you are going to upload the files as list instead of an array. So you need to change
the MultipartFile[] files into List<MultipartFile> in order to recieve the files as a list.

@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(value = "/examplefileupload/withrequestAsList",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithList(List<MultipartFile> files) {
    log.info("Request files as list " + files.size());
    // Add your processing logic here
    return ResponseEntity.ok("Success");
    }
}

So in here, there is no change in the postman request and only the postman URL should be changed
accordingly.


Spring boot multipart file upload as an Object

Think you need to upload a file with a description with custom title and description. So in
that case, you need to have a class which is having additional information.

public class FileDetails {
private String title;
private String description;
private MultipartFile file;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public MultipartFile getFile() {
return file;
}

public void setFile(MultipartFile file) {
this.file = file;
}
}

So let's create the rest API which is having the FileDetails as datatype. 

@Slf4j
@Controller
public class Example {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(value = "/examplefileupload/withrequestasobject",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
        public String uploadFilesExample7(FileDetails fileDetails) {
    log.info("File details " + fileDetails);
    return "success";
    }
}



So in the postman, you need to just create multiple files and every attribute in the
FileDetails should be used as an individual key to submit a request.



So in this tutorial, we discussed various occasions of how to upload a file and JSON data in
Postman and how we handle these. There are various methods of sending files to spring and you
need to have a good knowledge of how things are going with them. So let's wrap up this tutorial
and hope you understand most in this tutorial. See you in the next tutorial.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.