Overview
This endpoint handles image file uploads for blog posts. It accepts image files through multipart form data, validates the file type, and stores it in the images disk. The returned filename can then be used when creating or updating a post.
This endpoint requires authentication. You must include a valid JWT token in the Authorization header.
Endpoint
Authentication
Required. This endpoint uses the api.auth middleware to verify the JWT token.
Header:
Authorization: Bearer YOUR_JWT_TOKEN
Request Parameters
This endpoint expects multipart/form-data with a file upload.
The image file to upload. Must be a valid image file.Accepted formats: jpg, jpeg, png, gif
Example Request
Using cURL:
curl -X POST https://your-api.com/api/post/upload \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-F "file0=@/path/to/image.jpg"
Using JavaScript Fetch API:
const formData = new FormData();
formData.append('file0', fileInput.files[0]);
fetch('https://your-api.com/api/post/upload', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_JWT_TOKEN'
},
body: formData
})
.then(response => response.json())
.then(data => console.log(data));
Response
Success Response (200)
HTTP status code (200 for success)
Status message (“success”)
The filename of the uploaded image. This filename should be used when creating or updating posts.
{
"code": 200,
"status": "success",
"image": "1709564730example.jpg"
}
The filename is automatically generated by prepending the current Unix timestamp to the original filename. This prevents filename collisions.
Error Response (400) - Invalid File
HTTP status code (400 for bad request)
{
"code": 400,
"status": "error",
"message": "Error al subir la imagen"
}
This error is returned when:
- No file is provided
- The file is not a valid image
- The file format is not jpg, jpeg, png, or gif
Validation Rules
The endpoint validates the uploaded file with these rules:
file0: Required field
- Must be an image file
- Allowed MIME types: jpg, jpeg, png, gif
Implementation Details
The endpoint generates a unique filename and stores the image:
$image = $request->file('file0');
$image_name = time().$image->getClientOriginalName();
\Storage::disk('images')->put($image_name, \File::get($image));
Filename Generation
Filenames are generated using the pattern: {timestamp}{original_filename}
For example:
- Original:
photo.jpg
- Generated:
1709564730photo.jpg
Storage Location
Images are stored in the images disk as configured in Laravel’s config/filesystems.php.
Retrieving Uploaded Images
After uploading, images can be retrieved using the separate GET endpoint:
GET /api/post/image/{filename}
This endpoint:
- Does not require authentication
- Returns the image file directly with a 200 status
- Returns a 404 error if the image doesn’t exist
Example Image Retrieval
curl https://your-api.com/api/post/image/1709564730photo.jpg
The image retrieval endpoint (getImage) is publicly accessible and does not require authentication.
Workflow
- Upload an image using this endpoint
- Save the returned filename from the response
- Use the filename in the
image field when creating or updating a post
- Images can be displayed by constructing URLs:
/api/post/image/{filename}
- Create Post - Create a post using the uploaded image
- Update Post - Update a post with a new image
- GET /api/post/image/ - Retrieve an uploaded image (public endpoint)