Simple JSON Post Receiver Example
A NetBurner NNDK application that demonstrates how to receive JSON objects via HTTP POST requests from external RESTful APIs or JavaScript within web pages.
Overview
This application sets up a web server that can handle JSON POST requests through multiple endpoints. It provides both raw text processing and parsed JSON data handling capabilities, making it suitable for various integration scenarios.
Features
- Multiple POST Endpoints: Handles different types of POST requests
- JSON Parsing: Uses NetBurner's JSON lexer for structured data processing
- Raw Text Processing: Can handle plain text POST data
- Configuration Integration: Supports posting to the device's configuration system
- Web Interface: Includes a simple HTML interface with usage examples
POST Endpoints
The application provides three main POST endpoints:
1. /PostText
- Handles raw text POST data
- Processes content as plain text without JSON parsing
- Useful for simple data transmission
2. /PostLexer
- Processes JSON POST data using NetBurner's JSON lexer
- Validates JSON structure and provides lint checking
- Outputs parsed JSON object structure
3. /Config/AppData/MyOwnValue
- Allows posting directly to the configuration system
- Updates configuration values via HTTP POST
- Integrated with NetBurner's configuration server
Usage Examples
The application can be tested using the wget
command-line utility:
Send JSON to Text Processor
wget -O- --post-data='{"Data":1}' --header='Content-Type:application/json' 'http://[device-ip]/PostText'
Send JSON to Lexer Processor
wget -O- --post-data='{"Data":2}' --header='Content-Type:application/json' 'http://[device-ip]/PostLexer'
Update Configuration Value
wget -O- --post-data='{"MyOwnValue":8765}' --header='Content-Type:application/json' 'http://[device-ip]/Config/AppData/MyOwnValue'
Code Structure
Main Components
- **
main.cpp
**: Core application logic and POST handlers
- **
htmlvar.h
**: Header file for HTML variable functions
- **
index.html
**: Web interface with usage instructions
- **
ReadMe.txt
**: Original documentation
Key Functions
- **
GetHost()
**: Retrieves the host information from HTTP requests
- **
SendResponse()
**: Sends standardized HTTP responses
- **
MyJsonPostHandlerText()
**: Processes raw text POST data
- **
MyJsonPostHandler()
**: Processes parsed JSON data
Configuration
The application includes a configuration integer gMyOwnVal
with:
- Default value: 199
- Configuration path:
appdata
- Name: "MyOwnValue"
Technical Details
Dependencies
- NetBurner NNDK framework
- HTTP server functionality
- JSON lexer library
- Configuration server support
Network Setup
- Initializes network stack on startup
- Starts HTTP server on default port 80
- Waits for active network connection
- Enables configuration mirror for remote management
Error Handling
- JSON lint validation with error reporting
- Timeout handling for POST data reception
- Network connection status monitoring
Development Notes
- System diagnostics are enabled (should be removed for production)
- Uses NetBurner's real-time operating system (NBRTOS)
- Includes proper memory management and timeout handling
- Supports both synchronous and asynchronous POST processing
Getting Started
- Compile the application using NetBurner NNDK
- Deploy to your NetBurner device
- Access the web interface at
http://[device-ip]/
- Test POST endpoints using the provided wget examples
- Monitor console output for debugging information
Response Format
All POST requests return a simple HTML response indicating successful processing:
<html><body>Post has been Processed by [processor-name]</body></html>
This application serves as a foundation for building more complex JSON-based communication systems with NetBurner devices.