0
0
Postmantesting~15 mins

Form-data body in Postman - Build an Automation Script

Choose your learning style9 modes available
Send POST request with form-data body and verify response
Preconditions (3)
Step 1: Open Postman and create a new POST request
Step 2: Set the request URL to https://api.example.com/upload
Step 3: Select 'Body' tab and choose 'form-data' option
Step 4: Add key 'username' with value 'testuser'
Step 5: Add key 'file' and upload a sample file (e.g., sample.txt)
Step 6: Send the request
Step 7: Observe the response status code and body
✅ Expected Result: Response status code is 200 OK and response body contains confirmation message 'Upload successful for user testuser'
Automation Requirements - Postman (using pm scripting)
Assertions Needed:
Verify response status code is 200
Verify response body contains 'Upload successful for user testuser'
Best Practices:
Use pm.test and pm.response for assertions
Use descriptive test names
Keep test scripts simple and readable
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body contains confirmation message', () => {
    const responseJson = pm.response.json();
    pm.expect(responseJson.message).to.eql('Upload successful for user testuser');
});

The first test checks that the response status code is exactly 200, which means the request was successful.

The second test parses the response body as JSON and verifies that the 'message' field matches the expected confirmation text.

Using pm.test groups assertions with clear names, making test results easy to understand.

Common Mistakes - 3 Pitfalls
{'mistake': "Not setting the request body type to 'form-data' in Postman", 'why_bad': 'The API expects form-data format; sending raw JSON or other formats will cause the request to fail or behave unexpectedly.', 'correct_approach': "Always select 'form-data' in the Body tab when the API requires form-data."}
{'mistake': 'Not uploading the file correctly or forgetting to add the file key', 'why_bad': 'The API will not receive the file, causing the upload to fail or return an error.', 'correct_approach': "Add a key with type 'File' in form-data and upload the correct file."}
Using incorrect assertion syntax in Postman scripts
Bonus Challenge

Now add data-driven testing with 3 different usernames and files

Show Hint