</>

Technology

Postman

Difficulty

Intermediate

Interview Question

How do you handle file uploads in Postman?

Answer

File Upload in Postman

Postman supports file uploads using the form-data body type, which sends files as multipart/form-data.

Step-by-Step File Upload

  1. ✓Set method to POST (or PUT)
  2. ✓Enter the upload URL: https://api.example.com/upload
  3. ✓Click the Body tab
  4. ✓Select form-data radio button
  5. ✓Add a key field:
    • ✓Type: file (name of the file parameter the API expects)
    • ✓Hover over the key field → dropdown appears
    • ✓Change type from Text to File
  6. ✓Click "Select Files" in the Value column
  7. ✓Browse and select your file (PDF, image, CSV, etc.)
  8. ✓Click Send

Multiple Files

CODE
KEY         TYPE    VALUE
files       File    [Select document1.pdf]
files       File    [Select document2.pdf]
description Text    "Batch upload test"

File Upload with Additional Fields

CODE
KEY             TYPE    VALUE
file            File    [Select profile.jpg]
user_id         Text    12345
title           Text    "Profile Picture"
is_public       Text    true

Checking Upload Worked

JavaScript
// In Tests tab — validate upload response
pm.test("File uploaded successfully", function () {
    pm.response.to.have.status(200);
    const json = pm.response.json();
    pm.expect(json.file_id).to.be.a('string');
    pm.expect(json.url).to.match(/^https/);
    pm.expect(json.size).to.be.above(0);
});

pm.test("Filename is correct", function () {
    const json = pm.response.json();
    pm.expect(json.filename).to.include("profile");
});

Using Variables for File Paths

In Postman Desktop, you can use the pm.iterationData to pass file paths in Collection Runner:

JavaScript
// Pre-request Script
const filePath = pm.iterationData.get("file_path");
// Note: Postman doesn't support dynamic file paths via variables
// File selection must be done manually in form-data

Common Errors

ErrorCauseFix
400 Bad RequestWrong field nameMatch API's expected parameter name
413 Payload Too LargeFile too bigCheck API's max file size
415 Unsupported Media TypeWrong content typeUse form-data, not raw
File not attachedType is Text not FileChange key type to File

Testing Upload Limits

Test with:

  • ✓✅ Valid file types (PDF, JPG, PNG, CSV)
  • ✓✅ File at size limit (e.g., exactly 5MB)
  • ✓❌ Oversized file (e.g., 6MB when limit is 5MB) → expect 413
  • ✓❌ Invalid file type (e.g., .exe when only images allowed) → expect 400

Follow AutomateQA

Related Topics