Uploading a File and JSON Data in Postman – 在Postman中上传文件和JSON数据

最后修改: 2022年 10月 18日

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.

让我们建立一个基本的Spring Boot应用程序,暴露出上传数据的端点

2.1. Dependencies

2.1. 依赖性

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



2.2. Model

2.2 模式

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:


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:


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.

在这里,handleJsonInput(),我们期待一个JsonRequest类型的对象,我们定义的模型类。该方法返回一个200 HTTP状态代码,并在响应中提供输入的详细信息idname

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.

我们使用了注解@RequestBody ,该注解将输入反序列化为JsonRequest 对象。这样,我们已经看到了对JSON的简单处理,以验证输入。

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:

默认方法被设置为GET。所以一旦我们添加了localhost URL,我们需要选择POST作为方法:

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


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


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:

让我们添加localhost URL并选择POST作为方法,因为该方法默认为GET

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:


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:


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. 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.

在这篇文章中,我们建立了一个简单的Spring Boot应用程序,并研究了通过Postman向暴露的端点提供数据的两种不同方式。

As always, code samples are available over on GitHub.