Skip to content

How To Upload Image Using Raw In Postman

API Testing Blog

Uploading Images with Raw Body in Postman: A Step-by-Step Guide

When testing APIs that require image uploads, you often need to send the image data directly within the request body. This is where Postman’s “Raw” body option comes in handy.

1. Understanding the “Raw” Body Option in Postman

The “Raw” body option allows you to send data in its unprocessed form, including images. This lets you control the exact format and content of the request.

2. Choosing the Right Content Type

Before sending an image, you need to specify the correct “Content-Type” header. The most common content types for images are:

  • image/jpeg: For JPEG images.
  • image/png: For PNG images.
  • image/gif: For GIF images.

3. Preparing the Image Data

You can upload images to Postman in a few ways:

  • Drag and Drop: Simply drag the image file onto the Postman request window.
  • File Selector: Click the “Select Files” button to browse and choose an image from your local machine.
  • Base64 Encoding: Convert the image to a Base64 string and paste it into the “Raw” body.

4. Sending the Request with Raw Body

Step 1: Navigate to the “Body” tab in your Postman request. Step 2: Select “Raw” as the body type. Step 3: Choose the appropriate “Content-Type” from the dropdown menu. Step 4: Paste the image data into the body: * For Dragged/Selected Images: Postman will automatically handle the image data. * For Base64 Images: Paste the Base64 encoded string into the body.

Example:

Request URL: https://api.example.com/upload/image Method: POST Headers:

Content-Type: image/jpeg

Raw Body:

(Base64 encoded image data)

5. Testing and Validating Responses

After sending the request, check the response for success codes (e.g., 200, 201) and any validation messages. This will help you ensure that the image was successfully uploaded.

6. Troubleshooting:

  • Error 415 Unsupported Media Type: Ensure you’re using the correct “Content-Type” for the image format.
  • Empty Response: Check your image data and the URL/method used in the request.

7. Practical Example: Uploading an Image with Base64 Encoding

Here’s a complete example using Base64 encoding for a hypothetical image upload API:

Step 1: Convert the image to Base64:

Terminal window
# Using Python
import base64
with open("my_image.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
print(encoded_string)

Step 2: Copy the Base64 encoded string and paste it into the “Raw” body of your Postman request. Step 3: Set the “Content-Type” to image/jpeg.

Step 4: Send the request and observe the response.

8. Conclusion:

Using the “Raw” body type in Postman allows you to directly send image data within the request, giving you complete control over the upload process. By following these steps and understanding the associated best practices, you can ensure successful image uploads during your API testing.

API Testing Blog