Course – LS – NPI (cat=JSON/Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING

1. Overview

Postman is a popular API platform that optimizes the various steps of the API development lifecycle. Postman can be used to test our API without writing any code. We can use either the standalone app or the browser extension.

In this tutorial, we’ll see how to upload files and JSON data when using Postman.

2. Application Setup

Let’s set up a basic Spring Boot application that exposes endpoints to upload data.

2.1. Dependencies

We defined a basic spring application with spring-boot-starter-web dependency in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2. Model

Next, let’s define a simple model class for the JSON input:

public class JsonRequest {
    int id;
    String name;
}

For brevity, we’ve removed the declarations for constructors, getters/setters, etc.

2.3. Endpoints

Finally, let’s set up an endpoint as per the use case to handle requests as a file:

@PostMapping("/uploadFile")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file){
    return ResponseEntity.ok().body("file received successfully");
}

In the method handleFileUpload(), we expect a MultipartFile as input and subsequently return a 200 status message with a static text. We’ve kept it simple and haven’t explored saving or processing the input file.

The MultipartFile is provided by Spring-Web and it represents an uploaded file. This file is then stored in memory or temporarily on disk, which is subsequently flushed out once the request processing is complete.

Let’s also create an endpoint that handles JSON data:

@PostMapping("/uploadJson")
public ResponseEntity<String> handleJsonInput(@RequestBody JsonRequest json){
    return ResponseEntity.ok().body(json.getId()+json.getName());
}

Here, handleJsonInput(), we expect an object of type JsonRequest, the model class we’ve defined. The method returns a 200 HTTP status code with the input details id and name in the response.

We’ve used the annotation @RequestBody which deserializes the input into the JsonRequest object. This way, we’ve seen simplistic processing of the JSON to verify the input.

3. Uploading Data

We’ve set up the application and now let’s check the two ways to provide input to the application.

3.1. Uploading JSON Into Postman

JSON is one of the text input types for an endpoint. We’ll follow the below steps to pass the same to the exposed endpoint.

The default method is set to GET. So once we’ve added the localhost URL, we need to select POST as the method:

JsonStep1

Let’s click on the Body tab, then select raw. In the dropdown which displays Text, let’s select JSON as the input:

JsonStep2

We need to paste the input JSON and then click Send:

JsonStep3

We’re getting a 200 status code in response as we can see at the bottom of the snapshot. Additionally, the id and name from the input are returned in the response body, confirming that the JSON was processed correctly at the endpoint.

3.2. Uploading a File Into Postman

Let’s take a document file for our example here as we’ve not defined any constraints as to which file types can be consumed by the endpoint.

Let’s add the localhost URL and select POST as the method since the method defaults to GET:

FileUploadStep1

Let’s click on the Body tab, then select form-data. On the first row for the key-value pair, let’s click on the dropdown at the right corner for the key field, and select File as the input:

FileUploadStep2

We need to add the text file, which is our @RequestParam for the endpoint, in the key column and browse the desired file for the value column.

Finally, let’s click on Send:

FileUploadStep3

When we click on Send, we get a 200 HTTP status code with the static text defined in our endpoint definition. This means our file was successfully delivered to the endpoint with no errors or exceptions.

4. Sending Multipart Data and Json in the Same Request

Let’s see now how to send multipart data and JSON in the same request with Postman.

First, we need to configure our Postman request and set the JSON data we want to send in the pre-request script:

Pre request script to create the nested json

Second, we’ll go to the “Body” tab in the request editor and select “form-data” as the body type. Then we must add the fields for the nested JSON object and the multipart file:

  • For the nested JSON, we will get its value from the “jsonData” variable we defined earlier in the pre-request script. Also, we need to set its content type to application/json.
  • For the file field, we need to click on the “Choose Files” button, and a file dialog will open, allowing us to choose the desired file for upload. Once we’ve chosen the file, Postman will include it as part of the multipart/form-data request.
form data with a file and the nested json

Finally, let’s create an appropriate controller to handle the multipart/form-data request and process the file and nested JSON object accordingly.

@PostMapping("/uploadJsonAndMultipartData")
public ResponseEntity<String> handleJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) {
    return ResponseEntity.ok()
      .body(json.getId() + json.getName());
}

In this endpoint, the handleJsonAndMultipartInput() method accepts the nested JSON object as a JsonRequest object using @RequestPart(“data”), and the file is received as a MultipartFile object using @RequestPart(“file”).

5. Conclusion

In this article, we built a simple Spring Boot application and looked at two different ways to provide data to exposed endpoints via Postman.

As always, code samples are available over on GitHub.

Course – LS (cat=JSON/Jackson)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.