External Bodies

Load response bodies from external JSON or Excel files in moclojer. Perfect for large datasets, API proxies, and transforming spreadsheets into REST APIs with minimal configuration.

Instead of embedding large response bodies directly in YAML configuration files, moclojer supports loading responses from external files. This keeps your configuration clean and enables powerful use cases like transforming Excel spreadsheets into REST APIs or proxying remote endpoints.

🎯 Why Use External Bodies?

Problems with inline bodies:

  • ❌ Large JSON responses make YAML files hard to read

  • ❌ Difficult to maintain complex data structures

  • ❌ Version control diffs become messy

  • ❌ Can't reuse existing data files

Benefits of external bodies:

  • βœ… Keep YAML configuration clean and readable

  • βœ… Reuse existing JSON files and datasets

  • βœ… Proxy remote APIs without duplication

  • βœ… Transform Excel spreadsheets into APIs

  • βœ… Separate concerns (config vs data)

flowchart LR
    A[HTTP Request] --> B[moclojer]
    B --> C{External Body?}
    C -->|Yes| D{Provider Type}
    C -->|No| E[Use inline body]
    D -->|json| F[Load JSON file]
    D -->|xlsx| G[Load Excel file]
    F --> H[Parse JSON]
    G --> I[Convert to JSON]
    H --> J[Return Response]
    I --> J
    E --> J

πŸ“ Configuration

Replace body with external-body:

External Body Fields

Field
Required
Description

provider

Yes

File type: json or xlsx

path

Yes

File path (local or HTTP URL)

sheet-name

No

Excel sheet name (xlsx only)

🌍 Global Configuration

To avoid repeating the folder path in every endpoint, you can configure a global base folder for external bodies:

When to Use Global Configuration

Perfect for:

  • βœ… Docker/Kubernetes deployments with mounted volumes

  • βœ… Consistent folder structure across all endpoints

  • βœ… Reducing configuration duplication

  • βœ… Cleaner, more maintainable configuration

Example: Docker Compose Setup

Path Resolution Rules

When a global folder is configured:

  1. Relative paths (e.g., users.json) β†’ Combined with global folder

  2. Absolute paths (e.g., /tmp/data.json) β†’ Global folder is ignored

  3. URLs (e.g., https://...) β†’ Global folder is ignored

πŸ“„ JSON Provider

Local File

File: data/users.json

Request:

Response:

Remote URL (API Proxy)

Proxy responses from real APIs:

Request:

Response: (proxied from pokeapi.co)

Template Variables in Paths

Use template variables to make paths dynamic:

File structure:

πŸ“Š Excel (XLSX) Provider

Transform Excel spreadsheets into REST APIs:

Excel file: data/employees.xlsx

id
name
department
salary

1

Alice

Engineering

85000

2

Bob

Marketing

65000

3

Charlie

Sales

70000

Request:

Response:

Multiple Sheets

🌐 Real-World Use Cases

1. Product Catalog from Excel

Non-technical teams can update product data:

Benefits:

  • Business team updates Excel directly

  • No code changes needed

  • Version control for data files

  • Easy imports from existing systems

2. Multi-Environment API Proxy

3. Testing with Real Data Samples

File structure:

4. Financial Reports from Spreadsheets

πŸ”§ Advanced Patterns

Combining Template Variables and External Bodies

Fallback to Inline Body

When file doesn't exist, use inline body as fallback:

Conditional External Bodies

Use different files based on request:

Requests:

βœ… Best Practices

Do:

  • βœ… Use external bodies for responses > 50 lines

  • βœ… Organize files in logical directories (data/, mocks/, fixtures/)

  • βœ… Use meaningful file names (users.json, not data1.json)

  • βœ… Version control your data files alongside config

  • βœ… Use relative paths for portability

  • βœ… Use global configuration when all files are in the same folder

  • βœ… Document file structure in README

Don't:

  • ❌ Mix local and remote paths without clear documentation

  • ❌ Use absolute paths (breaks portability)

  • ❌ Store sensitive data in external files (use environment variables)

  • ❌ Forget to validate Excel sheet names (typos cause errors)

  • ❌ Use external bodies for tiny responses (overkill)

πŸ“ File Organization

πŸ§ͺ Testing External Bodies

Verify File Paths

Test with curl

Validate JSON Files

πŸ” Debugging

Common Issues

Issue
Solution

File not found

Check path is relative to moclojer working directory

Excel sheet not found

Verify sheet name matches exactly (case-sensitive)

Invalid JSON

Validate file with jq or online validator

Remote URL timeout

Check network connectivity, increase timeout

Template variable not replaced

Ensure variable exists in request

Enable Debug Logging

Check File Paths

πŸ“Š Performance Considerations

Provider
File Size
Load Time
Caching

JSON (local)

< 1MB

< 10ms

Yes

JSON (remote)

< 1MB

100-500ms

No (fresh each time)

XLSX (local)

< 5MB

50-200ms

Yes

Tips:

  • Keep local JSON files under 1MB for fast responses

  • Use remote URLs sparingly (network latency)

  • Large Excel files (> 10MB) may slow startup time

  • Consider caching for frequently accessed remote URLs

🚨 Important Notes

Path Resolution: Paths are relative to the directory where moclojer is executed, not the config file location.

Remote URLs: Fetched on every request (no caching). Use for dynamic data or infrequently accessed endpoints.

Excel Limitations: Only .xlsx format supported (not .xls or .csv). First row must be headers.

Template Variables: Work in path field but not inside the loaded file content.

πŸ“š See Also

Last updated

Was this helpful?