Skip to content

karanbangia/proto-http

Repository files navigation

Protocol Buffer HTTP Example

This project demonstrates a simple HTTP server that accepts JSON requests and returns Protocol Buffer responses.

Prerequisites

  1. Go 1.21 or later
  2. Protocol Buffer Compiler (protoc)

Installing Prerequisites

macOS

# Install Go
brew install go

# Install Protocol Buffer Compiler
brew install protobuf

Setup

  1. Clone the repository:
git clone https://github.com/your-username/proto-http.git
cd proto-http
  1. Install the Protocol Buffer compiler plugin for Go:
export PATH=$PATH:$(go env GOPATH)/bin && go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  1. Generate Protocol Buffer code:
chmod +x generate.sh
./generate.sh
  1. Install dependencies:
go mod tidy

Running the Server

Start the server:

go run main.go

The server will start on port 8080.

Testing the API

Using curl

  1. Get a user (returns protobuf):
curl http://localhost:8080/user?id=1
  1. Create a user (JSON input, protobuf output):
curl -X POST \
  http://localhost:8080/user \
  -H 'Content-Type: application/json' \
  -d '{
    "id": "2",
    "name": "Jane Doe",
    "email": "jane@example.com"
}'

Using Postman

  1. GET Request:

    • URL: http://localhost:8080/user?id=1
    • Method: GET
    • Response will be in protobuf format
  2. POST Request:

    • URL: http://localhost:8080/user
    • Method: POST
    • Headers: Content-Type: application/json
    • Body (raw JSON):
      {
        "id": "2",
        "name": "Jane Doe",
        "email": "jane@example.com"
      }
    • Response will be in protobuf format

Project Structure

  • api/user.proto: Protocol Buffer definition
  • server/server.go: HTTP server implementation
  • main.go: Server startup code
  • generate.sh: Script to generate Go code from proto files

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published