程序化创建Postman Collection时如何指定JSON请求体示例
Great question! I've run into this exact scenario when building Postman collections programmatically, and while the v2.1.0 spec doesn't have a dedicated "default JSON body" field, there are a few solid workarounds—including using JavaScript scripts like you suspected, plus some official Postman features you might not have considered.
Let's break down the options:
1. Use Pre-request Scripts to Auto-Populate the Request Body
This is the most direct way to get the "auto-fill on open" behavior you want. You can add a script to individual requests (or at the collection level for bulk behavior) that sets the raw request body to your default JSON structure.
For a single request, add this to the Pre-request Script tab:
// Define your default JSON payload const defaultRequestBody = { productId: 1234, name: "Sample Product", price: 99.99, inStock: true }; // Format the JSON with indentation for readability and set it to the request body pm.request.body.raw = JSON.stringify(defaultRequestBody, null, 2);
When the user opens the request and runs the script (or sends the request, which triggers the pre-request script automatically), the body will populate with your default values.
2. Combine Custom Collection Properties + Scripts for Scalable Defaults
If you're building the collection programmatically, you can add custom x- prefixed properties to your requests (Postman supports custom metadata like this) to store default bodies, then use a collection-level script to apply them.
First, when defining your request in the collection JSON, add a custom x-default-body property:
{ "name": "Create Product", "request": { "method": "POST", "url": "{{baseUrl}}/products", "body": { "mode": "raw", "raw": "" }, "x-default-body": { "productId": 1234, "name": "Sample Product", "price": 99.99 } } }
Then add this script to your collection's Pre-request Script tab to apply the default body to any request that has the custom property:
// Fetch the custom default body from the current request const defaultBody = pm.request.__parent.get("x-default-body"); if (defaultBody) { // Set the request body to the formatted JSON pm.request.body.raw = JSON.stringify(defaultBody, null, 2); // Optional: Save to an environment variable for easy editing pm.environment.set("currentRequestBody", JSON.stringify(defaultBody, null, 2)); }
This scales well if you have dozens of requests with unique default bodies—you just define the default once in the collection metadata, and the script handles the rest.
3. Use Postman's Built-in Examples Feature (Official Approach)
If you don't mind users manually loading the default body (instead of auto-filling), Postman's Examples feature is designed for this exact use case. When building your collection programmatically, you can attach an example to each request that includes your default JSON body.
Here's how to structure it in the collection JSON:
{ "name": "Create Product", "request": { "method": "POST", "url": "{{baseUrl}}/products", "body": { "mode": "raw", "raw": "" } }, "response": [ { "name": "Default Request Body", "originalRequest": { "method": "POST", "url": "{{baseUrl}}/products", "body": { "mode": "raw", "raw": "{\"productId\": 1234, \"name\": \"Sample Product\", \"price\": 99.99}" } }, "status": "OK", "code": 200, "_postman_previewlanguage": "json" } ] }
Users can then open the request, switch to the Examples tab, and click "Use Example" to load the default body into the request editor. This is the most "Postman-native" way to handle request templates, and it doesn't require any scripts.
Quick Note on the v2.1.0 Spec
You're right that the spec only defines the raw field as a string—no dedicated default value property. But that doesn't mean you can't set a default string during collection creation! If you don't need dynamic defaults, you can just write your default JSON directly into the raw field when building the collection:
"body": { "mode": "raw", "raw": "{\"productId\": 1234, \"name\": \"Sample Product\", \"price\": 99.99}" }
This is the simplest approach if your defaults are static.
内容的提问来源于stack exchange,提问作者Grokify

