Wednesday, May 1, 2024
HomeJavaMethods to add a file and JSON information in Postman? Instance Tutorial

Methods to add a file and JSON information in Postman? Instance Tutorial


Methods to add a file and JSON information in Postman is a typical query if you end up coping with REST APIs. As a way to do that, you should ship by means of POST request. So on this article, we’re going to clarify this utilizing totally different examples the place you possibly can add a single file in several methods. add an inventory of recordsdata, add as an object or add an inventory of objects containing pictures from postman. So earlier than shifting to the next totally different features of importing a single file in several methods, let’s assume that in your postman, you might be utilizing header named “Content material-Kind” with the worth of “multipart/form-data”.

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

                            The determine displaying the content-type multipart/form-data in postman.

Spring boot multipart file add instance

So
on this instance, we’re going to create a REST API which consumes
multipart request information. However to be able to do that, your utility should
have enter parameter which will get the multipartFile and this can auto get
by the spring. Following is an instance code, which applied the
state of affairs.

@Slf4j
@Controller
public class {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@PostMapping(worth = "/examplefileupload",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
public ResponseEntity<String> uploadFile(MultipartFile file) {
LOGGER.information("Request comprises, File: " + file.getOriginalFilename());
return ResponseEntity.okay("Success");
}

}
Now from the postman, ship the small print as follows. 

Spring boot multipart file add with @RequestParam 

This methodology is similar because the earlier mentioned instance, however solely distinction in right here is utilization of

@RequestParam annotation for enter argument.

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(worth = "/examplefileupload/withparameter",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithParameter(@RequestParam MultipartFile file) {
    log.information("File add with parameter:" + file.getOriginalFilename());
    return ResponseEntity.okay("Success");
    }
}
So in right here additionally working with above postmain request.

Spring boot multipart file add with @RequestPart

So in right here, there use a @RequestPart annotation because the enter argument. Others are stay

unchanged when examine to above examples.

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(worth = "/examplefileupload/withrequestpart",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithRequestPart(@RequestPart MultipartFile file) {
    log.information("Request comprises, File: " + file.getOriginalFilename());
    return ResponseEntity.okay("Success");
    }
}
So once more you needn't change the way in which how postman request occurs and it's precisely as above.


Spring boot multipart file add with @RequestBody

So on this instance, the @RequestBody is used to enter the file.

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(worth = "/examplefileupload/withrequestbody",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithRequestBody(@RequestBody MultipartFile file) {
    log.information("Request file with request physique" + file.getOriginalFilename());
    return ResponseEntity.okay("Success");
    }
}
There is no such thing as a change with the postman request and you may depend on the above postman request.
    

Spring boot multipart file add as an array

So in right here, we add the recordsdata as an array, Within the methodology enter argument, you should specify an array of MutliPartFile kind. 

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping(worth = "/examplefileupload/withrequestAsArray",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithArray(MultipartFile[] recordsdata) {
    log.information("Request recordsdata as an array" + recordsdata.size);
    return ResponseEntity.okay("Success");
    }
}

So in postman, we have to add a number of recordsdata and the recordsdata ought to have similar title because the
enter argument in relaxation api. Spring is accountable to transform these recordsdata into that inserted with
similar title. So given under of how we publish request utilizing the Postman.


Spring boot multipart file add as an inventory

So in right here, you'll add the recordsdata as checklist as a substitute of an array. So you should change

the MultipartFile[] recordsdata into Listing<MultipartFile> to be able to recieve the recordsdata as an inventory.

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());


    @PostMapping(worth = "/examplefileupload/withrequestAsList",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE} )
    public ResponseEntity<String> uploadFileWithList(Listing<MultipartFile> recordsdata) {
    log.information("Request recordsdata as checklist " + recordsdata.measurement());
    // Add your processing logic right here
    return ResponseEntity.okay("Success");
    }
}

So in right here, there isn't any change within the postman request and solely the postman URL ought to be modified
accordingly.

Spring boot multipart file add as an Object

Suppose you should add a file with an outline with customized title and outline. So in

that case, you should have a category which is having further data.

public class FileDetails {
personal String title;
personal String description;
personal 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 remainder API which is having the FileDetails as datatype. 

@Slf4j
@Controller
public class Instance {

personal remaining Logger LOGGER = LoggerFactory.getLogger(this.getClass());


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

So within the postman, you should simply create a number of recordsdata and each attribute within the
FileDetails ought to be used as a person key to submit a request.

So on this tutorial, we mentioned varied events of add a file and JSON information in
Postman and the way we deal with these. There are numerous strategies of sending recordsdata to spring and also you
must have data of how issues are going with them. So let's wrap up this tutorial
and hope you perceive most on this tutorial. See you within the subsequent tutorial.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments