Skip to content

How To Send File Using Postman

API Testing Blog

Sending Files with Postman: A Comprehensive Guide

Postman is a powerful tool for API testing that allows you to interact with APIs in various ways, including sending files. This capability is crucial for APIs that involve uploading or downloading files, like image upload services, document management systems, or file sharing platforms. Here’s a comprehensive guide on how to send files using Postman, encompassing different scenarios and techniques.

1. Using the Body Tab: The Fundamental Approach

The Body tab within Postman offers the fundamental method for file uploads.

Step 1: Select the Right HTTP Method

  • The most common HTTP method for file uploads is POST. Ensure that your API documentation specifies the correct method.

Step 2: Define the Endpoint

  • Input the full URL of the API endpoint that accepts file uploads.

Step 3: Set the Content-Type

  • Click the “Body” tab and choose “form-data” as the Content-Type.

Step 4: Add File Parameter

  • Click “Add file”, providing a descriptive “Key” (e.g., image, document).
  • Select the file from your computer using the “Value” field.
  • Optionally, you can include other form parameters in separate rows.

Example:

{
"key": "image",
"value": "C:\Users\YourName\Documents\image.jpg"
}

Step 5: Send the Request

  • Click the “Send” button.

2. Using the Headers Tab: Advanced Control

The Headers tab provides more granular control over the file upload process.

Step 1: Select the Right HTTP Method

  • Choose the appropriate HTTP method based on your API documentation.

Step 2: Define the Endpoint

  • Input the full URL of the API endpoint.

Step 3: Set the Content-Type

  • Click “Headers” and add a header with “Content-Type” as the key and “multipart/form-data” as the value.

Step 4: Add File Parameter

  • Switch back to the “Body” tab and choose “form-data”.
  • Add a file parameter as described in the previous section.

Step 5: Send the Request

  • Click the “Send” button.

3. Using the “raw” Tab: Precise Control

The “raw” tab gives you complete control over the request body, including the file data.

Step 1: Select the Right HTTP Method

  • Choose the appropriate HTTP method based on your API documentation.

Step 2: Define the Endpoint

  • Input the full URL of the API endpoint.

Step 3: Set the Content-Type

  • Click “Headers” and add a header with “Content-Type” as the key and “multipart/form-data” as the value.

Step 4: Construct the Request Body

  • Click “raw” tab and choose the “text” option.
  • Input the raw data for the file upload, adhering to the specific format required by the API (usually multipart/form-data encoding).

Step 5: Send the Request

  • Click the “Send” button.

Example (using Node.js client):

const FormData = require('form-data');
const axios = require('axios');
const formData = new FormData();
formData.append('image', fs.createReadStream('./image.jpg'), {
filename: 'image.jpg',
contentType: 'image/jpeg',
});
axios.post('https://api.example.com/upload', formData, {
headers: {
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
},
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});

4. Using Pre-Request Scripts: Automating File Selection

Postman’s pre-request scripts offer automation capabilities.

Step 1: Select the Right HTTP Method

  • Choose the required HTTP method as per your API documentation.

Step 2: Define the Endpoint

  • Input the full URL of the API endpoint.

Step 3: Choose the Content-Type

  • Set the content-type as “multipart/form-data”.

Step 4: Define the Pre-Request Script

  • In the “Pre-request Script” tab, set the file path variable. You might choose a constant path or use the pm.environment.get() function if the path is stored as an environment variable.

Step 5: Create Script

  • Write a script which defines the file path variable.
pm.environment.set("image_path", "./image.jpg");

Step 6: Send the Request

  • Click on the “Send” button.

5. Using Postman Collections: Organizing and Reusing File Upload Flows

Postman Collections allow you to organize and reuse file upload workflows.

Step 1: Create a Postman Collection

  • Create a new collection to group your file upload requests.

Step 2: Add Requests to the Collection

  • Add the file upload requests to the collection.

Step 3: Use Environment Variables for File Paths

  • Define environment variables for file paths within the collection to make the requests more dynamic.

Step 4: Run the Collection

  • Run the collection to execute the file upload requests.

6. File Downloading: Leveraging the “Response” Tab

Postman can also be used to download files from APIs.

Step 1: Select the Right HTTP Method

  • Choose a method like GET or POST based on your API’s download implementation.

Step 2: Define the Endpoint

  • Input the URL of the endpoint that provides the file for download.

Step 3: Analyze the Response

  • Click the “Send” button and view the response.
  • You’ll find the downloaded file in the “Response Body” section (often in a binary format).

Step 4: Save the File

  • You’ll typically need to use the “Save Response” feature in Postman to save the downloaded file to your local machine.

Note: Some APIs may require you to provide authentication or other specific headers for the download process. Always refer to the API documentation.

API Testing Blog