Skip to main content
In this guide, we walk you through testing the image upload endpoint of our Flask API. The endpoint now features enhanced image-loading, validation, robust error handling, and detailed logging. We use Postman Essentials to simulate various scenarios—including successful uploads and error conditions—to ensure the system behaves as expected. Below is an excerpt of the image upload endpoint. This snippet demonstrates how we manage file selection, validate file extensions, check file content using both imghdr and Pillow, and verify the quality parameter. Notice that after reading the file content, we reset the file pointer to ensure proper subsequent processing.

Running the Flask Server and Testing with Postman

Once the Flask server is running using flask run, open Postman Essentials and follow these steps:
  1. Select an Image: For this test, we use an image of the Northern Lights.
  2. Set the Parameters: In Postman, create a new request with the following details.
Below is an example request for a full-quality image upload:
This test confirms that the system only accepts specific file types—PNG, JPEG, JPG, and GIF—even when JPEG is a less common file extension.

Additional Upload Function Verification

The following snippet shows an alternative version of the upload function. This version first checks if the ‘image’ key exists, then validates the file extension and content:

Testing with a JPEG File

For a JPEG file upload, the request might look like this:

Testing with a PNG File

After selecting a PNG image (e.g., book.png), setting the quality parameter to 100 should display an acceptable image. However, reducing quality to 5 will result in poor output:

Testing with Another JPEG File

Similarly, testing with another JPEG file (coolgirl.jpeg) confirms that the quality adjustments work as expected:

Testing with a GIF File

When attempting to upload a GIF file, you may see a “failed to decode image” error. This outcome is expected because OpenCV’s imdecode function does not support GIF images:
A sample console output might be:
Since our application does not support GIF images, you will consistently see error messages for such files. For handling GIFs, consider using Pillow as recommended by BlackboxAI and Tabnine.

Enhanced Quality Parameter Validation

Below is an updated version of the upload function with improved quality parameter validation. This version ensures that the quality parameter is present and within the allowed range (0–100). If the parameter is omitted or invalid, an error message is returned:
After running Flask and testing the endpoint, if the quality parameter is missing you will receive:
A successful request with a valid quality value returns the processed image.

Final Version of the Flask Route

The final structure of our Flask route, incorporating all improvements, is shown below:
After integrating these enhancements, our Flask API is robust and ready for frontend consumption. Later, we will scaffold the frontend using Cursor alongside tools like Tabnine and GitHub Copilot.
The image shows a code editor with Python code for an image upload function, alongside a terminal displaying error logs related to HTTP requests.
When testing, check the API logs to see messages such as:
  • “Quality parameter is missing”
  • “Failed to decode image”
  • HTTP status codes 200 or 400 depending on the test scenario.
Thank you for following along. In the next article, we will build a React application to interact with this robust API. Happy coding!

Watch Video