如何从Rest API返回指定字段?SoapUI与Postman实现方法求助
Got it, let's break this down—restricting the fields returned by a GET API is a super common need, and both Postman and SoapUI make this easy depending on how the API is built. Here are the two main approaches, plus step-by-step for each tool:
1. Use API-Supported Field Selection (Best Practice)
Most well-designed REST APIs let you specify which fields to return via a query parameter. Common parameter names include fields, select, or projection—check the API docs if you're unsure which one to use.
Example Request URL
If your base endpoint is https://your-api-url.com/users/123, modify it to include the fields parameter:
https://your-api-url.com/users/123?fields=firstName,lastName,email
Postman Steps
- Open Postman, create a new GET request, and paste your API endpoint.
- Switch to the Params tab.
- Add a new key-value pair:
- Key:
fields(or the parameter your API uses) - Value:
firstName,lastName,email
- Key:
- Hit Send—your response should only include the three fields you specified.
SoapUI Steps
- Open SoapUI, create a new REST project using your API endpoint.
- Select the GET request from the project tree on the left.
- In the request editor, find the Params section (below the URL bar).
- Click the
+button to add a new parameter:- Name:
fields - Value:
firstName,lastName,email
- Name:
- Click Send, and you’ll get the filtered response.
2. Filter the Response in the Tool (If API Doesn’t Support Field Selection)
If the API doesn’t let you specify fields via a query parameter, you can use each tool’s scripting feature to extract only the fields you need after receiving the full response.
Postman Steps
- Send the full GET request first to get all fields.
- Go to the Tests tab of your request.
- Paste this script to filter the response:
// Parse the full JSON response const fullResponse = pm.response.json(); // Extract only the fields you want const filteredResponse = { firstName: fullResponse.firstName, lastName: fullResponse.lastName, email: fullResponse.email }; // Replace the original response with the filtered one pm.response.setBody(JSON.stringify(filteredResponse, null, 2));
- Send the request again—your Response tab will now show only the three fields.
SoapUI Steps
- Send the full GET request to retrieve all fields.
- Switch to the Script tab of the request.
- Paste this Groovy script to filter the response:
// Parse the full JSON response def fullResponse = new groovy.json.JsonSlurper().parseText(context.response) // Create a new object with your desired fields def filteredResponse = [ firstName: fullResponse.firstName, lastName: fullResponse.lastName, email: fullResponse.email ] // Update the response to show only the filtered fields context.response = new groovy.json.JsonBuilder(filteredResponse).toPrettyString()
- Run the request again, and the response panel will display only the fields you picked.
Pro tip: Always check the API documentation first—using the API’s built-in field selection is more efficient than filtering on the client side, since it reduces the amount of data transferred.
内容的提问来源于stack exchange,提问作者Umair Khan

