xmlrpc is an implementation of client side part of XML-RPC protocol in Go.
This project is in minimal maintenance mode with no further development. Bug fixes are accepted, but it might take some time until they are merged.
go get github.com/ninech/xmlrpcclient, err := xmlrpc.NewClientWithOptions("https://bugzilla.mozilla.org/xmlrpc.cgi")
if err != nil {
log.Fatal(err)
}
defer client.Close()
var result struct {
Version string `xmlrpc:"version"`
}
if err := client.Call("Bugzilla.version", nil, &result); err != nil {
log.Fatal(err)
}
fmt.Printf("Version: %s\n", result.Version) // Version: 4.2.7+Use CallContext for requests with timeout or cancellation:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var result string
if err := client.CallContext(ctx, "App.status", nil, &result); err != nil {
log.Fatal(err)
}Configure the client with functional options:
client, err := xmlrpc.NewClientWithOptions(url,
xmlrpc.WithBasicAuth("username", "password"),
xmlrpc.WithHeader("User-Agent", "my-app/1.0"),
)Available options:
WithHTTPClient(*http.Client)- use a custom HTTP clientWithTransport(http.RoundTripper)- set a custom transportWithHeader(key, value string)- add a header to all requestsWithBasicAuth(user, pass string)- set basic authenticationWithCookieJar(http.CookieJar)- set a custom cookie jar
xmlrpc supports encoding of native Go data types to method arguments.
Data types encoding rules:
int,int8,int16,int32,int64encoded tointfloat32,float64encoded todoubleboolencoded tobooleanstringencoded tostringtime.Timeencoded todateTime.iso8601xmlrpc.Base64encoded tobase64- slices encoded to
array
Structs are encoded to struct by the following rules:
- all public fields become struct members
- field name becomes member name
- if field has
xmlrpctag, its value becomes member name - for fields tagged with
omitempty, empty values are omitted - fields tagged with
-are omitted
Example:
type Book struct {
Title string `xmlrpc:"title"`
Author string `xmlrpc:"author,omitempty"`
ISBN string `xmlrpc:"-"`
}Server methods can accept multiple arguments. To handle this case, use a slice
of []any. Each value of such slice is encoded as a separate argument.
Result of remote function is decoded to native Go data type.
Data types decoding rules:
int,i4decoded toint,int8,int16,int32,int64doubledecoded tofloat32,float64booleandecoded toboolstringdecoded tostringarraydecoded to slicestructdecoded following the rules described in previous sectiondateTime.iso8601decoded totime.Timebase64decoded tostring
Run unit tests:
go test ./...Run integration tests (requires Docker):
docker-compose up -d
go test -tags integration ./...
docker-compose downSee project status.
Dmitry Maksimov (dmtmax@gmail.com)