How to Upload a File Attachment From a REST API Post Request

Tip: use multipart-form-data in your Java code

Ryan James
Better Programming
Published in
3 min readMay 15, 2020

--

Photo by Drew Farwell on Unsplash

After hours and hours of searching for a way to download file attachments from an API POST request, I finally put together bits and pieces of different Stack Overflow and blog posts to get it working.

This should not be that hard. And honestly, it wasn’t.

I used this inside of my Spring project, but the logic will still be the same across other types of Java projects.

I will also show you how to test the endpoint using Postman.

Objective

The objective is to download a file attachment sent from an API POST request.

If you would like to follow along, I suggest using the following tools:

Create Your REST Endpoint

We are first going to set up our REST endpoint. It is a simple PostMapping which will reside in our Controller.java class.

--

--

Director of Engineering @ Startup. Giving back to the dev community one article at a time! Need a website? https://ggilabs.com