Home » Laravel » How to Upload File in Laravel API Using Postman

How to Upload File in Laravel API Using Postman

In many cases, you may need the ability to upload one or multiple files to a Laravel application. If you use API, you can do it in various ways. Many APIs require files to be encoded as base64 string and sent as POST field. But it is not very convenient for testing and validation.

You can send and receive files in binary format as multipart/form-data. In this short article, I will show you how to upload a file in Laravel API using Postman.

How to Upload File in Laravel API Using Postman

I assume you have installed and deployed a sample Laravel project that you can use for experiments. If not, look through this article. Let’s prepare code for handling file uploading on the server side. We need a FormRequest class that will be used for validation and a controller class. You can create the request class using this command:

php artisan make:request UploadRequest

This command will generate a class with the following code in the app/Http/Requests directory:

<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UploadRequest extends FormRequest { public function authorize() { return false; } public function rules() { // } }

You can add any validation in the rules() method. For example, make the file field required:

public function rules() { return [ "file" => "required", ]; }

Then, create a controller class using this command:

php artisan make:controller UploadController

The controller class will be created in the app/Http/Controllers folder. Add the __invode() method that prints all received files for debugging:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UploadController extends Controller { public function __invoke(Request $request) { dd($request->allFiles()); } }

And finally, register the controller in the routes/api.php file:

Route::post("upload", \App\Http\Controllers\UploadController::class);

This controller is invokable so you don’t need to specify the action name here. Run the local development server using the command below, and you are ready to upload files:

php artisan serve

1. Upload Single File

Create a new request, change the request method to POST and add any name. If you have registered your route in the routes/api.php file use this URL: http://localhost:8000/api/upload or http://localhost:8000/upload when you put your route into the routes/web.php file:

Then go to the Body tab and select the form-data for the type list:

Add the field and name it “file”, then find an arrow near the end of the input. Click on it and choose the File option:

After this, you will see the Select Files button in the value input which can be used to upload files. Choose the file and press the Send button. If everything is fine, you will see debug information about your file:

2. Multiple Files

You can send multiple files as easily as a single file. Postman allows you to add a few files using the Select Files button. But you need to make one change. Rename the field name to file[]. In this case, Laravel will read this variable as an array and you will be able to get them all. For example:

Wrapping Up

In this short article, I have explained how to upload files to Laravel API using Postman. As you can see it is elementary and Postman supports all required functionality by default.

Leave a Comment

Exit mobile version