Skip to main content

Endpoint

method
string
default:"POST"
POST
endpoint
string
/api/category
This endpoint requires authentication. Include a valid authorization token in the request.

Authentication

This endpoint is protected by the api.auth middleware. You must include a valid authorization token in the request headers.

Request Body

The request body must contain a json parameter with a JSON string containing the category data.
json
string
required
JSON string containing category data
name
string
required
The name of the category (required)

Validation Rules

  • name: Required field

Response

Returns a JSON object containing the created category or an error message.

Success Response

code
number
HTTP status code (200 for success)
status
string
Response status (‘success’)
category
object
The created category object
id
number
Category ID (auto-generated)
name
string
Category name
created_at
string
Timestamp when the category was created
updated_at
string
Timestamp when the category was last updated

Error Response

code
number
HTTP status code (400 for validation error)
status
string
Response status (‘error’)
message
string
Error message describing what went wrong

Example Request

cURL
curl -X POST http://your-domain.com/api/category \
  -H "Authorization: Bearer YOUR_TOKEN_HERE" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d 'json={"name":"Technology"}'

Example Responses

Success Response (200)

{
  "code": 200,
  "status": "success",
  "category": {
    "id": 1,
    "name": "Technology",
    "created_at": "2024-01-15T10:30:00.000000Z",
    "updated_at": "2024-01-15T10:30:00.000000Z"
  }
}

Validation Error (400)

{
  "code": 400,
  "status": "error",
  "message": "No se ha guardado la categoria"
}

Empty Request Error (400)

{
  "code": 400,
  "status": "error",
  "message": "No has enviado ninguna categoria"
}

Implementation Details

This endpoint is defined in CategoryController.php:50. It:
  1. Receives data as a JSON string in the json parameter
  2. Validates that the name field is present (defined at line 58-60)
  3. Creates a new Category model instance and saves it to the database
  4. Excludes index and show methods from authentication (middleware defined at line 16)
The request expects data to be sent as a form parameter named json containing a JSON string, not as direct JSON in the request body.

Build docs developers (and LLMs) love