kitex-contrib / kitexcall

A command line tool for sending requests using kitex
Apache License 2.0
15 stars 5 forks source link

kitexcall (This is a community driven project)

English | 中文

Kitexcall is a command-line tool for sending JSON general requests using kitex, similar to how curl is used for HTTP.

Features

Installation

go install github.com/kitex-contrib/kitexcall@latest

Usage

Basic Usage

When using the kitexcall tool, you need to specify several required arguments, including the path to the IDL file, the method name, and the data to be sent. Example:

// echo.thrift

namespace go api

struct Request {
    1: string message
}

struct Response {
    1: string message
}

service Echo {
    Response echo(1: Request req)
}
{
    "message": "hello"
}
var _ api.Echo = &EchoImpl{}

// EchoImpl implements the last service interface defined in the IDL.
type EchoImpl struct{}

// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
    klog.Info("echo called")
    return &api.Response{Message: req.Message}, nil
}

func main() {
    svr := echo.NewServer(new(EchoImpl))
    if err := svr.Run(); err != nil {
        log.Println("server stopped with error:", err)
    } else {
        log.Println("server stopped")
    }
}
kitexcall -idl-path echo.thrift -m echo -d '{"message": "hello"}' -e 127.0.0.1:9999

Output:

[Status]: Success
{
    "message": "hello"
}
kitexcall -idl-path echo.thrift -m echo -e 127.0.0.1:9999 -f input.json

Output:

[Status]: Success
{
    "message": "hello"
}

Command Line Options

Detailed Description

IDL Type

Use the -type or -t flag to specify the IDL type. Supported types are thrift and protobuf, with a default of thrift.

kitexcall -t thrift

IDL Path

Use the -idl-path or -p flag to specify the path to the IDL file.

kitexcall -idl-path /path/to/idl/file.thrift

Method to Call (Required)

Use the -method or -m flag to specify the method name. The format can be IDLServiceName/MethodName or just MethodName. When the server side has MultiService mode enabled, IDLServiceName must be specified, and the transport protocol must be TTHeader or TTHeaderFramed.

kitexcall -m GenericService/ExampleMethod
kitexcall -m ExampleMethod

Request Data

Use the -data or -d flag to specify the data to be sent, which should be a JSON formatted string. Alternatively, use the -file or -f flag to specify the path to a JSON file containing the data.

Assuming we want to send the data {"message": "hello"}, we can specify the data like this:

kitexcall -m ExampleMethod -d '{"message": "hello"}'

Or, we can save the data in a JSON file, such as input.json, and then specify the file path:

kitexcall -m ExampleMethod -f input.json

Server Address

Use the -endpoint or -e flag to specify one or more server addresses.

Assuming we have two server addresses, 127.0.0.1:9919 and 127.0.0.1:9920, we can specify the server addresses like this:

kitexcall -m ExampleMethod -e 127.0.0.1:9919 -e 127.0.0.1:9920

Metadata

Assuming we want to pass Transient metadata temp=temp-value and persistent metadata logid=12345 to the server and receive backward metadata, we can specify it like this:

kitexcall -m ExampleMethod -meta temp=temp-value -meta-persistent logid=12345 -meta-backward

Business Exceptions

If the server returns a business exception, you can enable the client to receive business custom exception error codes, error messages, and additional information through the -biz-error flag.

Assuming the server returns a business error status code 404 and message not found, we can enable business error handling like this:

kitexcall -m ExampleMethod -biz-error

Enable Verbose Mode

Use the -verbose or -v flag to enable verbose mode, providing more detailed output information.

Maintained by: Zzhiter