Json2Protobuf is a lightweight proxy service that enables seamless JSON to Protocol Buffers conversion for HTTP requests and responses. It allows you to dynamically convert between JSON and Protobuf message formats without modifying your existing services.
- Convert JSON to Protocol Buffers for request payloads
- Optional conversion of upstream service responses back to JSON
- Flexible message type specification
- Support for GET, POST, and other HTTP methods
- Detailed error reporting
- Configurable logging
- Go 1.16+
- Protocol Buffer .proto files defining your message types
go install github.com/sashahilton00/json2protobuf@latest
json2protobuf --proto-dir=/path/to/proto/definitions [options]
--proto-dir
: Directory containing .proto files (required)--addr
: Listening address (default: localhost)--port
: Listening port (default: 3000)--log-level
: Logging level (debug, info, warn, error)
X-J2P-Destination-Host
: Target upstream service URL (required)X-J2P-Method-Type
: Override HTTP method for upstream requestX-J2P-Request-Message-Type
: Protobuf message type for request payloadX-J2P-Response-Message-Type
: Protobuf message type for response conversion
curl http://localhost:3000/users \
-H "X-J2P-Destination-Host=user-service.example.com" \
-H "X-J2P-Response-Message-Type=UserList"
curl http://localhost:3000/users \
-X POST \
-H "X-J2P-Destination-Host=user-service.example.com" \
-H "X-J2P-Request-Message-Type=CreateUserRequest" \
-H "X-J2P-Response-Message-Type=User" \
-d '{"username": "johndoe", "email": "[email protected]"}'
When conversion fails, the proxy returns a JSON error response with:
- Error description
- Detailed message
- Problematic field (if applicable)
Logs are color-coded and include request/response details at the specified log level.