Skip to content

How To Send A File Using Postman

API Testing Blog

How to Send a File Using Postman

Postman is a powerful tool for API testing, and it allows you to send files as part of your requests. This is especially useful when working with APIs that require file uploads, such as image uploads, document uploads, or data file submissions.

This guide will walk you through the process of sending files using Postman, covering various techniques and scenarios.

Understanding File Upload Methods

Before diving into the specifics, let’s understand the core methods for uploading files using APIs:

  • Form Data: This is the most common method. You send the file as a part of a multipart/form-data request. Postman uses this method by default for file uploads.
  • Body (Raw): You can send the file as a raw request body using specific content types like application/json or text/plain. However, this requires special handling to encode the file data.
  • URL-Encoded Form: Similar to form data, but you send the file as a URL encoded parameter. This is less common for file uploads due to limitations in size and encoding.

Method 1: Sending a File Using Form Data

  1. Create a Request: In Postman, create a new request using the desired HTTP method (POST, PUT, etc.).
  2. Select the Body Tab: Navigate to the “Body” tab in Postman.
  3. Choose “form-data”: In the “Body” tab, choose the “form-data” option.
  4. Add File Parameter: Click the “Add File” icon (looks like an upload symbol).
  5. Specify File Details:
    • Key: The name of the parameter that represents the file in the API (e.g., ‘image’, ‘document’).
    • Value: Select the file from your local machine using the file browser button.
  6. Add Other Parameters: You can add more parameters to the form data (text, numbers, etc.) if required by your API.
  7. Send the Request: Click the “Send” button to execute the request.

Example:

API Endpoint: https://api.example.com/upload

Form Data Structure:

  • Key: file
  • Value: The selected image file (sample.jpg)

Code Example:

POST https://api.example.com/upload
Form Data:
file: [selected file: sample.jpg]

Method 2: Sending a File Using Body (Raw)

This method involves encoding the file contents into a specific format before sending. It’s more complex than form data, but it gives you more control over the request.

  1. Create Request: Set up your request in Postman as described in method 1.
  2. Select “raw”: In the “Body” tab, choose the “raw” option.
  3. Select Content Type: Choose the appropriate content type. For example, application/json if your API expects JSON data.
  4. Encode the File: Use coding libraries (e.g., Python, JavaScript) to encode the file contents into the selected format.
  5. Paste the Encoded Data: Copy and paste the encoded data into the “raw” body area in Postman.
  6. Send Request: Send the request as usual.

Example:

API Endpoint: https://api.example.com/upload-data

Content Type: application/json

Encoded File Data:

{
"name": "sample-file.txt",
"data": "base64 encoded file contents"
}

Code Example (JavaScript):

// Load the file
const file = document.getElementById('fileInput').files[0];
// Convert the file to base64
const reader = new FileReader();
reader.onload = function(e) {
const base64Data = e.target.result;
// Create the JSON object with encoded data
const jsonData = {
name: 'sample-file.txt',
data: base64Data
};
// Send the request using Postman
// ... use Postman to send the JSON object to the API
};
reader.readAsDataURL(file);

Method 3: Sending Multiple Files

You can send multiple files using the form-data method by adding multiple file parameters.

  1. Create Request: Follow the steps for Method 1 to create your request.

  2. Add Multiple Files: Create multiple “file” parameters within the form data section, each with a distinct key and value (file). For example:

    • Key: image1

    • Value: The first image file (image1.jpg)

    • Key: image2

    • Value: The second image file (image2.png)

  3. Send Request: Send the request as usual.

Method 4: Sending Files with Authorization

Many APIs require authorization for file uploads. Postman can handle this easily:

  1. Set Up Authorization:
    • Authorization Tab: Go to the “Authorization” tab in Postman.
    • Select Type: Choose the authorization type suitable for your API (e.g., Basic Auth, Bearer Token, API Key).
    • Provide Credentials: Fill in the necessary credentials (username, password, token, etc.).
  2. Send File: Follow the steps in Method 1 or Method 2 for sending the file, ensuring that the authorization is set up correctly.

Example:

API Endpoint: https://api.example.com/protected/upload

Authorization: Bearer Token (token stored in the “Authorization” tab in Postman)

Code Example:

Postman Authorization Tab:

  • Type: Bearer Token
  • Token: your_token_here

Request Body:

POST https://api.example.com/protected/upload
Form Data:
file: [selected file: sample.pdf]

Method 5: Sending Files with Custom Headers

Some APIs might require you to send specific headers with file uploads.

  1. Headers Tab: Navigate to the “Headers” tab in Postman.

  2. Add Headers: Add custom headers with their corresponding values. For example:

    • Key: Content-Type

    • Value: multipart/form-data

    • Key: X-Custom-Header

    • Value: some_value

  3. Send Request: Send the request as usual, ensuring the headers are set correctly.

By understanding these various methods, you can effectively send files using Postman for API testing. Remember to consult the API documentation for specific requirements and parameters relating to file uploads.

API Testing Blog