This is an implementation of client-side part of XML-RPC protocol in Go.
Add dependency to your project:
go get -u alexejk.io/go-xmlrpc
Use it by creating an *xmlrpc.Client
and firing RPC method calls with Call()
.
package main
import(
"fmt"
"alexejk.io/go-xmlrpc"
)
func main() {
client, _ := xmlrpc.NewClient("https://bugzilla.mozilla.org/xmlrpc.cgi")
defer client.Close()
result := &struct {
BugzillaVersion struct {
Version string
}
}{}
_ = client.Call("Bugzilla.version", nil, result)
fmt.Printf("Version: %s\n", result.BugzillaVersion.Version)
}
Customization is supported by passing a list of Option
to the NewClient
function.
For instance:
http.Client
used to perform requests, use HttpClient
option, otherwise http.DefaultClient
will be usedHeaders
option.SkipUnknownFields(true)
option (default is false
)Arguments to the remote RPC method are passed on as a *struct
. This struct is encoded into XML-RPC types based on following rules:
int
(encoded as <int>
) or float64
(encoded as <double>
)Response is decoded following similar rules to argument encoding.
SkipUnknownFields
option).map[string]any
, in case struct member names are not known at compile time. Map keys are enforced to string
type.If XML-RPC response contains no value for well-known data-types, it will be decoded into the default "empty" values as per table below:
XML-RPC Value | Default Value |
---|---|
<string/> |
"" |
<int/> , <i4/> |
0 |
<boolean/> |
false |
<double/> |
0.0 |
<dateTime.iso8601/> |
time.Time{} |
<base64/> |
nil |
<array><data/><array> |
nil |
As per XML-RPC specification, <struct>
may not have an empty list of <member>
elements, thus no default "empty" value is defined for it.
Similarly, <array/>
is considered invalid.
XML-RPC specification does not necessarily specify any rules for struct's member names. Some services allow struct member names to include characters not compatible with standard Go field naming.
To support these use-cases, it is possible to remap the field by use of struct tag xmlrpc
.
For example, if a response value is a struct that looks like this:
<struct>
<member>
<name>stringValue</name>
<value><string>bar</string></value>
</member>
<member>
<name>2_numeric.Value</name>
<value><i4>2</i4></value>
</member>
</struct>
it would be impossible to map the second value to a Go struct with a field 2_numeric.Value
as it's not valid in Go.
Instead, we can map it to any valid field as follows:
v := &struct {
StringValue string
SecondNumericValue string `xmlrpc:"2_numeric.Value"`
}{}
Similarly, request encoding honors xmlrpc
tags.
To build this project, simply run make all
.
If you prefer building in Docker instead - make build-in-docker
is your friend.