When building a Laravel API, it’s common to allow users to upload a file (like an image or document) alongside other task details such as the title or due date. Here’s how you can test that functionality using Postman.
🔧 Requirements
Ensure your Laravel setup:
- Accepts
multipart/form-data
requests. - Handles file uploads using
$request->file('attachment')->store(...)
.
✅ Step-by-Step: Testing in Postman
1. Choose the API Method
Set the HTTP method to POST
or PUT
depending on whether you're creating or updating a resource.
Example endpoint:
POST http://localhost:8000/api/tasks
2. Go to the Body Tab
- Select
form-data
. - Add key-value pairs for your fields.
3. Enter the Fields
Use a minimal and common set of fields:
Key | Value | Type |
---|---|---|
title |
Submit report | Text |
date |
2025-05-05 | Text |
attachment |
(Choose a file) | File |
👉 Make sure attachment
is of type File. The rest should be type Text.
4. Add Authorization (If Required)
If your API requires a token:
- Go to the Authorization tab.
- Select Bearer Token and paste your token there.
🧠 Backend Handling
In your Laravel controller, the attachment handling might look like this:
if ($request->hasFile('attachment')) {
$filePath = $request->file('attachment')->store('attachments', 'public');
$task->attachment_path = $filePath;
}
This stores the file in storage/app/public/attachments
and saves the path in the database.
✅ Final Tips
- Always test using
form-data
(notraw
JSON) when uploading files. - Validate the file using Laravel’s
mimes
andmax
rules. - Don't forget to run
php artisan storage:link
to make attachments accessible via the public URL.
With this approach, you can easily test and upload files alongside basic task fields in your Laravel API.