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
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:
Relative paths (e.g.,
users.json) β Combined with global folderAbsolute paths (e.g.,
/tmp/data.json) β Global folder is ignoredURLs (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
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, notdata1.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
Recommended Structure
π§ͺ Testing External Bodies
Verify File Paths
Test with curl
Validate JSON Files
π Debugging
Common Issues
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
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
.xlsxformat supported (not.xlsor.csv). First row must be headers.
Template Variables: Work in
pathfield but not inside the loaded file content.
π See Also
Template Variables - Using variables in file paths
Path Parameters - Dynamic file loading
Multi-Domain Support - Different files per environment
Real-World Example - Complete examples
Last updated
Was this helpful?