← Back to Services

eCommerce Service gRPC-Web API - Automation Guide

This document provides curl commands for testing and automating the eCommerce Service gRPC-Web API.

Endpoints

gRPC vs gRPC-Web

This service supports both native gRPC and gRPC-Web protocols:

Native gRPC (Direct Connection)

gRPC-Web (via Envoy Proxy)

Accessing via Envoy Proxy

When using grpcurl to access services through Envoy proxy on port 443, you need to include the x-service-target header for reflection queries:

# List available services (requires x-service-target header)
grpcurl -insecure -H 'x-service-target: ecommerce' \
  grpc-envoy.qa.akamai.com:443 list

# Call a service method (requires x-service-target header for reflection)
grpcurl -insecure -H 'x-service-target: ecommerce' \
  -d '{"page_size": 10}' \
  grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetProducts

Why the header? Since each backend service has its own gRPC reflection endpoint, the x-service-target header tells Envoy which service to route reflection queries to. This enables grpcurl to discover the service schema before making the actual call.

Note: The -servername flag is required when using localhost with TLS to match the certificate's domain name.

Prerequisites

All requests use: -H "x-service-target: ecommerce" \ - Content-Type: application/grpc-web+proto - X-Grpc-Web: 1 - X-User-Agent: grpc-web-javascript/0.1

Python Helper Script

First, create a helper script to generate protobuf messages:

cat > /tmp/ecommerce_grpc_helper.py << 'EOF'
import struct
import sys

def encode_varint(value):
    result = []
    while value >= 0x80:
        result.append((value & 0x7F) | 0x80)
        value >>= 7
    result.append(value)
    return bytes(result)

def encode_string_field(field_number, value):
    string_bytes = value.encode('utf-8')
    tag = (field_number << 3) | 2  # Wire type 2 (length-delimited)
    return encode_varint(tag) + encode_varint(len(string_bytes)) + string_bytes

def encode_int32_field(field_number, value):
    tag = (field_number << 3) | 0  # Wire type 0 (varint)
    return encode_varint(tag) + encode_varint(value)

def encode_double_field(field_number, value):
    tag = (field_number << 3) | 1  # Wire type 1 (fixed64)
    return encode_varint(tag) + struct.pack('<d', value)

def encode_bool_field(field_number, value):
    tag = (field_number << 3) | 0  # Wire type 0 (varint)
    return encode_varint(tag) + encode_varint(1 if value else 0)

def create_grpc_frame(message):
    return b'\x00' + struct.pack('>I', len(message)) + message

def create_get_products(page=1, page_size=20, category="", sort_by="name", min_price=0, max_price=0, featured_only=False):
    message = (
        encode_int32_field(1, page) +
        encode_int32_field(2, page_size) +
        encode_string_field(3, category) +
        encode_string_field(4, sort_by) +
        encode_double_field(5, min_price) +
        encode_double_field(6, max_price) +
        encode_bool_field(7, featured_only)
    )
    return create_grpc_frame(message)

def create_get_product(product_id):
    message = encode_string_field(1, product_id)
    return create_grpc_frame(message)

def create_search_products(query, limit=20, category="", min_price=0, max_price=0):
    message = (
        encode_string_field(1, query) +
        encode_int32_field(2, limit) +
        encode_string_field(3, category) +
        encode_double_field(4, min_price) +
        encode_double_field(5, max_price)
    )
    return create_grpc_frame(message)

def create_search_suggestions(partial_query, max_suggestions=5):
    message = (
        encode_string_field(1, partial_query) +
        encode_int32_field(2, max_suggestions)
    )
    return create_grpc_frame(message)

def create_add_to_cart(cart_id, product_id, quantity):
    message = (
        encode_string_field(1, cart_id) +
        encode_string_field(2, product_id) +
        encode_int32_field(3, quantity)
    )
    return create_grpc_frame(message)

def create_remove_from_cart(cart_id, product_id):
    message = (
        encode_string_field(1, cart_id) +
        encode_string_field(2, product_id)
    )
    return create_grpc_frame(message)

def create_get_cart(cart_id):
    message = encode_string_field(1, cart_id)
    return create_grpc_frame(message)

def create_update_cart_item(cart_id, product_id, new_quantity):
    message = (
        encode_string_field(1, cart_id) +
        encode_string_field(2, product_id) +
        encode_int32_field(3, new_quantity)
    )
    return create_grpc_frame(message)

def create_clear_cart(cart_id):
    message = encode_string_field(1, cart_id)
    return create_grpc_frame(message)

def create_get_categories():
    return create_grpc_frame(b'')  # Empty message

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python ecommerce_grpc_helper.py <command> [args...]")
        sys.exit(1)

    command = sys.argv[1]

    if command == "get_products":
        page = int(sys.argv[2]) if len(sys.argv) > 2 else 1
        category = sys.argv[3] if len(sys.argv) > 3 else ""
        data = create_get_products(page=page, category=category)
    elif command == "get_product":
        if len(sys.argv) != 3:
            print("Usage: python ecommerce_grpc_helper.py get_product <product_id>")
            sys.exit(1)
        data = create_get_product(sys.argv[2])
    elif command == "search_products":
        if len(sys.argv) < 3:
            print("Usage: python ecommerce_grpc_helper.py search_products <query> [category]")
            sys.exit(1)
        query = sys.argv[2]
        category = sys.argv[3] if len(sys.argv) > 3 else ""
        data = create_search_products(query, category=category)
    elif command == "search_suggestions":
        if len(sys.argv) != 3:
            print("Usage: python ecommerce_grpc_helper.py search_suggestions <partial_query>")
            sys.exit(1)
        data = create_search_suggestions(sys.argv[2])
    elif command == "add_to_cart":
        if len(sys.argv) != 5:
            print("Usage: python ecommerce_grpc_helper.py add_to_cart <cart_id> <product_id> <quantity>")
            sys.exit(1)
        data = create_add_to_cart(sys.argv[2], sys.argv[3], int(sys.argv[4]))
    elif command == "remove_from_cart":
        if len(sys.argv) != 4:
            print("Usage: python ecommerce_grpc_helper.py remove_from_cart <cart_id> <product_id>")
            sys.exit(1)
        data = create_remove_from_cart(sys.argv[2], sys.argv[3])
    elif command == "get_cart":
        if len(sys.argv) != 3:
            print("Usage: python ecommerce_grpc_helper.py get_cart <cart_id>")
            sys.exit(1)
        data = create_get_cart(sys.argv[2])
    elif command == "update_cart_item":
        if len(sys.argv) != 5:
            print("Usage: python ecommerce_grpc_helper.py update_cart_item <cart_id> <product_id> <new_quantity>")
            sys.exit(1)
        data = create_update_cart_item(sys.argv[2], sys.argv[3], int(sys.argv[4]))
    elif command == "clear_cart":
        if len(sys.argv) != 3:
            print("Usage: python ecommerce_grpc_helper.py clear_cart <cart_id>")
            sys.exit(1)
        data = create_clear_cart(sys.argv[2])
    elif command == "get_categories":
        data = create_get_categories()
    else:
        print(f"Unknown command: {command}")
        sys.exit(1)

    sys.stdout.buffer.write(data)
EOF

Protobuf Response Parser

Add this helper script to decode gRPC-Web responses into readable protobuf:

cat > /tmp/parse_ecommerce_response.py << 'EOF'
import struct
import sys
import subprocess
import os

def parse_grpc_web_response(response_file, message_type, proto_file="ecommerce-service/proto/ecommerce.proto"):
    """Parse gRPC-Web binary response and decode using protoc"""

    # Read the response data
    with open(response_file, "rb") as f:
        data = f.read()

    if len(data) < 5:
        print("Response too short")
        return

    # Parse gRPC-Web frames
    i = 0
    frame_count = 0

    while i < len(data):
        if i + 5 > len(data):
            break

        flag = data[i]
        n = struct.unpack(">I", data[i+1:i+5])[0]
        i += 5

        if i + n > len(data):
            break

        body = data[i:i+n]
        i += n

        if flag == 0:  # data frame (uncompressed)
            frame_file = f"/tmp/frame{frame_count}.pb"
            with open(frame_file, "wb") as f:
                f.write(body)

            print(f"=== Frame {frame_count} (Data) ===")

            # Use protoc to decode the protobuf message
            try:
                result = subprocess.run([
                    "protoc", 
                    f"--proto_path={os.path.dirname(proto_file)}/", 
                    "--decode", message_type, 
                    proto_file
                ], input=body, capture_output=True)

                if result.returncode == 0:
                    print(result.stdout.decode('utf-8'))
                else:
                    print(f"Protoc decode error: {result.stderr.decode('utf-8')}")
                    print(f"Raw hex: {body.hex()}")
            except FileNotFoundError:
                print("protoc not found, showing raw hex:")
                print(f"Raw hex: {body.hex()}")

            frame_count += 1

        elif flag == 0x80:  # trailer frame
            print(f"=== Frame {frame_count} (Trailers) ===")
            trailer_text = body.decode('utf-8', errors='ignore')
            print(trailer_text)
            frame_count += 1

def parse_streaming_response(response_file, message_type, proto_file="ecommerce-service/proto/ecommerce.proto"):
    """Parse streaming gRPC-Web response (for search suggestions)"""

    with open(response_file, "rb") as f:
        data = f.read()

    i = 0
    frame_count = 0

    while i < len(data):
        if i + 5 > len(data):
            break

        flag = data[i]
        n = struct.unpack(">I", data[i+1:i+5])[0]
        i += 5

        if i + n > len(data):
            break

        body = data[i:i+n]
        i += n

        if flag == 0:  # data frame
            print(f"=== Suggestion {frame_count} ===")

            try:
                result = subprocess.run([
                    "protoc", 
                    f"--proto_path={os.path.dirname(proto_file)}/", 
                    "--decode", message_type, 
                    proto_file
                ], input=body, capture_output=True)

                if result.returncode == 0:
                    print(result.stdout.decode('utf-8'))
                else:
                    print(f"Raw hex: {body.hex()}")
                    print(f"stdout: {bytes.fromhex(body.hex()).decode("utf-8")}")
            except FileNotFoundError:
                print(f"Raw hex: {body.hex()}")

            frame_count += 1

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python parse_ecommerce_response.py <response_file> <message_type> [proto_file]")
        print("Examples:")
        print("  python parse_ecommerce_response.py /tmp/response.bin ecommerce.GetProductsResponse")
        print("  python parse_ecommerce_response.py /tmp/response.bin ecommerce.SearchSuggestion --streaming")
        sys.exit(1)

    response_file = sys.argv[1]
    message_type = sys.argv[2]
    proto_file = sys.argv[3] if len(sys.argv) > 3 else "ecommerce-service/proto/ecommerce.proto"
    streaming = "--streaming" in sys.argv

    if streaming:
        parse_streaming_response(response_file, message_type, proto_file)
    else:
        parse_grpc_web_response(response_file, message_type, proto_file)
EOF

Basic Operations

1. Get Categories

Method A: Native gRPC (Direct to Service)

# List all available services
grpcurl -plaintext localhost:50052 list

# Get categories - simplest method
grpcurl -plaintext localhost:50052 ecommerce.ECommerceService/GetCategories

# Using proto file explicitly
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext localhost:50052 \
  ecommerce.ECommerceService/GetCategories

Method B: gRPC via Envoy Proxy (TLS)

# Via HTTPS proxy (production)
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{}' grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetCategories

# Via local Envoy proxy
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{}' localhost:8080 ecommerce.ECommerceService/GetCategories

Method C: gRPC-Web via curl

# Create the request
python3 /tmp/ecommerce_grpc_helper.py get_categories > /tmp/get_categories.bin

# Send the request and save response
curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetCategories \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_categories.bin \
  --output /tmp/get_categories_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/get_categories_response.bin ecommerce.GetCategoriesResponse

2. Get Products (with filtering and pagination)

Method A: Native gRPC (Direct to Service)

# Get all products (default pagination)
grpcurl -plaintext localhost:50052 ecommerce.ECommerceService/GetProducts

# Get products with specific parameters
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "page": 1,
  "page_size": 10,
  "category": "electronics",
  "sort_by": "price_asc",
  "min_price": 100.0,
  "max_price": 1000.0,
  "featured_only": false
}' localhost:50052 ecommerce.ECommerceService/GetProducts

# Get featured electronics only
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "page": 1,
  "page_size": 5,
  "category": "electronics",
  "featured_only": true
}' localhost:50052 ecommerce.ECommerceService/GetProducts

Method B: gRPC via Envoy Proxy (TLS)

# Via HTTPS proxy
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{
  "page": 1,
  "page_size": 10,
  "category": "electronics"
}' grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetProducts

# Via local Envoy proxy
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "page": 1,
  "page_size": 5,
  "featured_only": true
}' localhost:8080 ecommerce.ECommerceService/GetProducts

Method C: gRPC-Web via curl

# Get first page of electronics products
python3 /tmp/ecommerce_grpc_helper.py get_products 1 "electronics" > /tmp/get_products.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetProducts \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_products.bin \
  --output /tmp/get_products_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/get_products_response.bin ecommerce.GetProductsResponse

3. Get Single Product

Method A: Native gRPC (Direct to Service)

# Get product by ID
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"product_id": "1"}' localhost:50052 ecommerce.ECommerceService/GetProduct

# Get different products
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"product_id": "iphone-14-pro"}' localhost:50052 ecommerce.ECommerceService/GetProduct
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"product_id": "21"}' localhost:50052 ecommerce.ECommerceService/GetProduct

Method B: gRPC via Envoy Proxy (TLS)

# Via HTTPS proxy
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{"product_id": "1"}' grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetProduct

# Via local Envoy proxy
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"product_id": "3"}' localhost:8080 ecommerce.ECommerceService/GetProduct

Method C: gRPC-Web via curl

# Get product with ID "1"
python3 /tmp/ecommerce_grpc_helper.py get_product "1" > /tmp/get_product.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetProduct \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_product.bin \
  --output /tmp/get_product_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/get_product_response.bin ecommerce.Product

4. Search Products

Method A: Native gRPC (Direct to Service)

# Basic search
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"query": "iPhone"}' localhost:50052 ecommerce.ECommerceService/SearchProducts

# Advanced search with filters
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "query": "iPhone",
  "limit": 10,
  "category": "electronics",
  "min_price": 500.0,
  "max_price": 1500.0
}' localhost:50052 ecommerce.ECommerceService/SearchProducts

# Search in specific category
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "query": "laptop",
  "category": "electronics",
  "limit": 5
}' localhost:50052 ecommerce.ECommerceService/SearchProducts

Method B: gRPC via Envoy Proxy (TLS)

# Via HTTPS proxy
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{
  "query": "iPhone",
  "category": "electronics"
}' grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/SearchProducts

# Via local Envoy proxy
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "query": "laptop",
  "limit": 10
}' localhost:8080 ecommerce.ECommerceService/SearchProducts

Method C: gRPC-Web via curl

# Search for "iPhone" products in electronics category
python3 /tmp/ecommerce_grpc_helper.py search_products "iPhone" "electronics" > /tmp/search_products.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/SearchProducts \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/search_products.bin \
  --output /tmp/search_products_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/search_products_response.bin ecommerce.SearchProductsResponse

5. GetSearchSuggestions (Streaming)

Get real-time search suggestions as the user types. This endpoint uses server-side streaming to provide dynamic autocomplete functionality.

gRPC Definition:

rpc GetSearchSuggestions(SearchSuggestionsRequest) returns (stream SearchSuggestion);

message SearchSuggestionsRequest {
  string partial_query = 1;
  int32 max_suggestions = 2;
}

message SearchSuggestion {
  string suggestion = 1;
  int32 result_count = 2;
  string category = 3;
}

Method A: Direct gRPC (grpcurl with reflection)

# Get suggestions for partial query "iP"
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"partial_query": "iPhone"}' \
  localhost:50052 ecommerce.ECommerceService/GetSearchSuggestions

# Get suggestions with limit
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "partial_query": "Sam",
  "max_suggestions": 5
}' localhost:50052 ecommerce.ECommerceService/GetSearchSuggestions

# Get more suggestions
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "partial_query": "laptop",
  "max_suggestions": 10
}' localhost:50052 ecommerce.ECommerceService/GetSearchSuggestions

Method B: gRPC via Envoy (grpcurl with TLS)

# External access through Envoy proxy
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{"partial_query": "iP"}' \
  grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetSearchSuggestions

# Search suggestions with limit
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{
  "partial_query": "electronics",
  "max_suggestions": 3
}' grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetSearchSuggestions

Method C: gRPC-Web via HTTP (curl - streaming)

# Get search suggestions (streaming response)
echo '{"partial_query": "iP"}' | \
curl -k -N -X POST https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetSearchSuggestions \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+json" \
  -H "X-Grpc-Web: 1" \
  --data-binary @-

# Get suggestions with limit
echo '{
  "partial_query": "Sam",
  "max_suggestions": 5
}' | curl -k -N -X POST https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetSearchSuggestions \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+json" \
  -H "X-Grpc-Web: 1" \
  --data-binary @-

Sample Streaming Response:

{"suggestion": "iPhone", "resultCount": 45, "category": "electronics"}
{"suggestion": "iPad", "resultCount": 23, "category": "electronics"}
{"suggestion": "iPod", "resultCount": 12, "category": "electronics"}

Cart Operations

6. GetCart

Retrieve the current cart for a user, including all items and their quantities.

gRPC Definition:

rpc GetCart(GetCartRequest) returns (GetCartResponse);

message GetCartRequest {
  string cart_id = 1;
}

message GetCartResponse {
  Cart cart = 1;
  bool success = 2;
  string error_message = 3;
}

message Cart {
  string cart_id = 1;
  repeated CartItem items = 2;
  double total_price = 3;
  int32 total_items = 4;
}

message CartItem {
  string product_id = 1;
  string name = 2;
  double price = 3;
  int32 quantity = 4;
  double subtotal = 5;
}

Method A: Direct gRPC (grpcurl with reflection)

# Get cart for user
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123"}' \
  localhost:50052 ecommerce.ECommerceService/GetCart

# Get different user's cart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_alice"}' \
  localhost:50052 ecommerce.ECommerceService/GetCart

# Get cart that doesn't exist (should return empty cart)
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_nonexistent"}' \
  localhost:50052 ecommerce.ECommerceService/GetCart

Method B: gRPC via Envoy (grpcurl with TLS)

# External access through Envoy proxy
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123"}' \
  grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetCart

# Get cart with error handling
grpcurl -insecure -H 'x-service-target: ecommerce' -d '{"cart_id": ""}' \
  grpc-envoy.qa.akamai.com:443 ecommerce.ECommerceService/GetCart

Method C: gRPC-Web via HTTP (curl)

# Get cart for user
echo '{"cart_id": "cart_user123"}' | \
curl -k -X POST https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetCart \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+json" \
  -H "X-Grpc-Web: 1" \
  --data-binary @-

# Get different user's cart
echo '{"cart_id": "cart_alice"}' | \
curl -k -X POST https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetCart \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+json" \
  -H "X-Grpc-Web: 1" \
  --data-binary @-

Sample Response:

{
  "cart": {
    "cartId": "cart_user123",
    "items": [
      {
        "productId": "1",
        "name": "iPhone 15 Pro",
        "price": 999.99,
        "quantity": 1,
        "subtotal": 999.99
      },
      {
        "productId": "5",
        "name": "AirPods Pro",
        "price": 249.99,
        "quantity": 2,
        "subtotal": 499.98
      }
    ],
    "totalPrice": 1499.97,
    "totalItems": 3
  },
  "success": true
}

curl -s -X POST \ https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetCart \ -H "Content-Type: application/grpc-web+proto" \ -H "X-Grpc-Web: 1" \ -H "X-User-Agent: grpc-web-javascript/0.1" \ --data-binary @/tmp/get_cart.bin \ --output /tmp/get_cart_response.bin

Parse the response

python3 /tmp/parse_ecommerce_response.py /tmp/get_cart_response.bin ecommerce.Cart

#### Native gRPC (Direct to Service)
```bash
# Get cart by ID
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123"}' localhost:50052 ecommerce.ECommerceService/GetCart

# Get a different cart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_demo_001"}' localhost:50052 ecommerce.ECommerceService/GetCart

# Using proto file
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext -d '{"cart_id": "cart_user123"}' \
  localhost:50052 ecommerce.ECommerceService/GetCart

7. Add Item to Cart

gRPC-Web (via Envoy Proxy)

# Add 2 units of product "1" to cart "cart_user123"
python3 /tmp/ecommerce_grpc_helper.py add_to_cart "cart_user123" "1" 2 > /tmp/add_to_cart.bin

curl -v -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/AddToCart \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/add_to_cart.bin \
  --output /tmp/add_to_cart_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/add_to_cart_response.bin ecommerce.AddToCartResponse

Native gRPC (Direct to Service)

# Add item to cart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "cart_id": "cart_user123",
  "product_id": "1", 
  "quantity": 2
}' localhost:50052 ecommerce.ECommerceService/AddToCart

# Add multiple different items
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123", "product_id": "iphone-14-pro", "quantity": 1}' localhost:50052 ecommerce.ECommerceService/AddToCart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123", "product_id": "samsung-galaxy-s23", "quantity": 3}' localhost:50052 ecommerce.ECommerceService/AddToCart

# Using proto file
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext -d '{"cart_id": "cart_user123", "product_id": "1", "quantity": 2}' \
  localhost:50052 ecommerce.ECommerceService/AddToCart

8. Update Cart Item Quantity

gRPC-Web (via Envoy Proxy)

# Update product "1" quantity to 5 in cart "cart_user123"
python3 /tmp/ecommerce_grpc_helper.py update_cart_item "cart_user123" "1" 5 > /tmp/update_cart.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/UpdateCartItem \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/update_cart.bin \
  --output /tmp/update_cart_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/update_cart_response.bin ecommerce.UpdateCartItemResponse

Native gRPC (Direct to Service)

# Update cart item quantity
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "cart_id": "cart_user123",
  "product_id": "1",
  "new_quantity": 5
}' localhost:50052 ecommerce.ECommerceService/UpdateCartItem

# Set quantity to 1
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123", "product_id": "iphone-14-pro", "new_quantity": 1}' localhost:50052 ecommerce.ECommerceService/UpdateCartItem

# Using proto file
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext -d '{"cart_id": "cart_user123", "product_id": "1", "new_quantity": 5}' \
  localhost:50052 ecommerce.ECommerceService/UpdateCartItem

9. Remove Item from Cart

gRPC-Web (via Envoy Proxy)

# Remove product "1" from cart "cart_user123"
python3 /tmp/ecommerce_grpc_helper.py remove_from_cart "cart_user123" "1" > /tmp/remove_from_cart.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/RemoveFromCart \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/remove_from_cart.bin \
  --output /tmp/remove_from_cart_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/remove_from_cart_response.bin ecommerce.RemoveFromCartResponse

Native gRPC (Direct to Service)

# Remove item from cart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{
  "cart_id": "cart_user123",
  "product_id": "1"
}' localhost:50052 ecommerce.ECommerceService/RemoveFromCart

# Remove different products
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123", "product_id": "iphone-14-pro"}' localhost:50052 ecommerce.ECommerceService/RemoveFromCart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123", "product_id": "samsung-galaxy-s23"}' localhost:50052 ecommerce.ECommerceService/RemoveFromCart

# Using proto file
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext -d '{"cart_id": "cart_user123", "product_id": "1"}' \
  localhost:50052 ecommerce.ECommerceService/RemoveFromCart

10. Clear Cart

gRPC-Web (via Envoy Proxy)

# Clear all items from cart "cart_user123"
python3 /tmp/ecommerce_grpc_helper.py clear_cart "cart_user123" > /tmp/clear_cart.bin

curl -s -X POST \
  https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/ClearCart \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/clear_cart.bin \
  --output /tmp/clear_cart_response.bin

# Parse the response
python3 /tmp/parse_ecommerce_response.py /tmp/clear_cart_response.bin ecommerce.ClearCartResponse

Native gRPC (Direct to Service)

# Clear cart
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_user123"}' localhost:50052 ecommerce.ECommerceService/ClearCart

# Clear different carts
grpcurl -plaintext -H 'x-service-target: ecommerce' -d '{"cart_id": "cart_demo_001"}' localhost:50052 ecommerce.ECommerceService/ClearCart

# Using proto file
grpcurl -proto ecommerce-service/proto/ecommerce.proto \
  -plaintext -d '{"cart_id": "cart_user123"}' \
  localhost:50052 ecommerce.ECommerceService/ClearCart

Complete eCommerce Automation Script

Here's a complete script that demonstrates the full eCommerce workflow:

#!/bin/bash
# ecommerce_automation.sh

ENDPOINT="https://grpc-envoy.qa.akamai.com"
CART_ID="demo_cart_$(date +%s)"

echo "=== eCommerce Service Automation Demo ==="
echo "Using cart ID: $CART_ID"

# Step 1: Get available categories
echo "1. Getting available categories..."
python3 /tmp/ecommerce_grpc_helper.py get_categories > /tmp/get_categories.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/GetCategories" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_categories.bin \
  --output /tmp/get_categories_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/get_categories_response.bin ecommerce.GetCategoriesResponse
echo

# Step 2: Browse products
echo "2. Browsing electronics products..."
python3 /tmp/ecommerce_grpc_helper.py get_products 1 "electronics" > /tmp/get_products.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/GetProducts" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_products.bin \
  --output /tmp/get_products_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/get_products_response.bin ecommerce.GetProductsResponse
echo

# Step 3: Search for specific products
echo "3. Searching for iPhone products..."
python3 /tmp/ecommerce_grpc_helper.py search_products "iPhone" > /tmp/search_products.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/SearchProducts" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/search_products.bin \
  --output /tmp/search_products_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/search_products_response.bin ecommerce.SearchProductsResponse
echo

# Step 4: Get search suggestions
echo "4. Getting search suggestions for 'iP'..."
python3 /tmp/ecommerce_grpc_helper.py search_suggestions "iP" > /tmp/search_suggestions.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/GetSearchSuggestions" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/search_suggestions.bin \
  --output /tmp/search_suggestions_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/search_suggestions_response.bin ecommerce.SearchSuggestion --streaming
echo

# Step 5: Add items to cart
echo "5. Adding iPhone to cart (2 units)..."
python3 /tmp/ecommerce_grpc_helper.py add_to_cart "$CART_ID" "1" 2 > /tmp/add_to_cart1.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/AddToCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/add_to_cart1.bin \
  --output /tmp/add_to_cart1_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/add_to_cart1_response.bin ecommerce.AddToCartResponse
echo "iPhone added to cart"

# Step 6: Add another product
echo "6. Adding Samsung Galaxy to cart (1 unit)..."
python3 /tmp/ecommerce_grpc_helper.py add_to_cart "$CART_ID" "2" 1 > /tmp/add_to_cart2.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/AddToCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/add_to_cart2.bin \
  --output /tmp/add_to_cart2_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/add_to_cart2_response.bin ecommerce.AddToCartResponse
echo "Samsung Galaxy added to cart"

# Step 7: Check cart contents
echo "7. Checking cart contents..."
python3 /tmp/ecommerce_grpc_helper.py get_cart "$CART_ID" > /tmp/get_cart.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/GetCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_cart.bin \
  --output /tmp/get_cart_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/get_cart_response.bin ecommerce.Cart
echo

# Step 8: Update item quantity
echo "8. Updating iPhone quantity to 3..."
python3 /tmp/ecommerce_grpc_helper.py update_cart_item "$CART_ID" "1" 3 > /tmp/update_cart.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/UpdateCartItem" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/update_cart.bin \
  --output /tmp/update_cart_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/update_cart_response.bin ecommerce.UpdateCartItemResponse
echo "iPhone quantity updated"

# Step 9: Remove one item
echo "9. Removing Samsung Galaxy from cart..."
python3 /tmp/ecommerce_grpc_helper.py remove_from_cart "$CART_ID" "2" > /tmp/remove_from_cart.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/RemoveFromCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/remove_from_cart.bin \
  --output /tmp/remove_from_cart_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/remove_from_cart_response.bin ecommerce.RemoveFromCartResponse
echo "Samsung Galaxy removed from cart"

# Step 10: Final cart check
echo "10. Final cart check..."
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/GetCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/get_cart.bin \
  --output /tmp/get_cart_final_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/get_cart_final_response.bin ecommerce.Cart
echo

# Step 11: Clear cart
echo "11. Clearing cart..."
python3 /tmp/ecommerce_grpc_helper.py clear_cart "$CART_ID" > /tmp/clear_cart.bin
curl -s -X POST "$ENDPOINT/ecommerce.ECommerceService/ClearCart" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/clear_cart.bin \
  --output /tmp/clear_cart_response.bin
python3 /tmp/parse_ecommerce_response.py /tmp/clear_cart_response.bin ecommerce.ClearCartResponse
echo "Cart cleared"

echo "=== eCommerce Demo Complete ==="

Response Format

All responses are in gRPC-Web format: - First 5 bytes: gRPC frame header (compression flag + 4-byte length) - Remaining bytes: Protobuf message data - Trailers: gRPC status and message headers

Error Handling

Make Scripts Executable

chmod +x /tmp/ecommerce_grpc_helper.py
chmod +x /tmp/parse_ecommerce_response.py
chmod +x ecommerce_automation.sh

Example Usage

# Create the helper scripts first
python3 /tmp/ecommerce_grpc_helper.py get_categories > /tmp/test.bin

# Test protobuf parsing
curl -s -X POST https://grpc-envoy.qa.akamai.com/ecommerce.ECommerceService/GetCategories \
  -H "x-service-target: ecommerce" \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -H "X-User-Agent: grpc-web-javascript/0.1" \
  --data-binary @/tmp/test.bin \
  --output /tmp/test_response.bin

python3 /tmp/parse_ecommerce_response.py /tmp/test_response.bin ecommerce.GetCategoriesResponse

# Run the full automation
./ecommerce_automation.sh

Sample Parsed Output

The protobuf parser will show readable output like:

=== Frame 0 (Data) ===
categories {
  id: "electronics"
  name: "Electronics"
  description: "Electronic devices and gadgets"
  icon: "📱"
  product_count: 10
}
categories {
  id: "clothing"
  name: "Clothing"
  description: "Fashion and apparel"
  icon: "👕"
  product_count: 5
}

=== Frame 1 (Trailers) ===
grpc-status:0
grpc-message:

For streaming search suggestions:

=== Suggestion 0 ===
suggestion: "iPhone"
type: PRODUCT_NAME
result_count: 1

=== Suggestion 1 ===
suggestion: "iPad"
type: PRODUCT_NAME
result_count: 1

This automation script demonstrates: 1. ✅ Browse product categories 2. ✅ Search and filter products 3. ✅ Get real-time search suggestions 4. ✅ Add multiple items to cart 5. ✅ Update cart item quantities 6. ✅ Remove items from cart 7. ✅ Calculate cart totals with tax 8. ✅ Clear cart when done