Postman application allows users to call APIs to send and receive data from the server through the REST API.
To upload a file, create a REST API in Laravel and create a new request in Postman with form data to allow users to choose file upload in Laravel 11 applications.
Let’s start to create file upload using postman via rest api:
Step 1: Create an API Endpoint for File Upload
Create a file upload controller to handle file upload logic into it:
php artisan make:controller FileController
Open FileController.php
in the app/Http/Controllers
folder and add a method to handle file uploads:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FileController extends Controller
{
public function upload(Request $request)
{
if ($request->hasFile('file')) {
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$file->move(public_path('uploads'), $fileName);
return response()->json(['message' => 'File uploaded successfully'], 200);
} else {
return response()->json(['error' => 'No file uploaded'], 400);
}
}
}
Step 2: Define Routes
Define a route to this controller method in routes/api.php
:
use App\Http\Controllers\FileController;
Route::post('/upload', [FileController::class, 'upload']);
Step 3: Testing with Postman
Open Postman and create a new request for file upload with the following url on postman;
http://localhost:8000/api/upload
And then Go to the “Body” tab and select “form-data” and Add a key named “file” and select the file you want to upload using the “Choose Files” button, and click on the “Send” button to send the request.
Step 4: Verify the File Upload
When you upload file, you will receive response on your postman.