Documentation
¶
Overview ¶
Package xmlrpc provides an XML-RPC client implementation for Go.
The package implements the client side of the XML-RPC protocol, allowing Go programs to make remote procedure calls to XML-RPC servers.
Basic usage:
client, err := xmlrpc.NewClientWithOptions("https://example.com/xmlrpc",
xmlrpc.WithHeader("User-Agent", "my-app/1.0"),
xmlrpc.WithBasicAuth("user", "pass"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
var result string
err = client.Call("Method.Name", arg, &result)
Example ¶
package main
import (
"fmt"
"log"
"github.com/ninech/xmlrpc"
)
func main() {
client, err := xmlrpc.NewClientWithOptions("https://example.com/xmlrpc",
xmlrpc.WithHeader("User-Agent", "my-app/1.0"),
)
if err != nil {
log.Fatal(err)
}
defer func() { _ = client.Close() }()
var result struct {
Version string `xmlrpc:"version"`
}
if err := client.Call("App.version", nil, &result); err != nil {
log.Fatal(err)
}
fmt.Printf("Version: %s\n", result.Version)
}
Index ¶
- Variables
- func EncodeMethodCall(method string, args ...any) ([]byte, error)
- func NewRequest(url string, method string, args any) (*http.Request, error)
- func NewRequestContext(ctx context.Context, url string, method string, args any) (*http.Request, error)
- type Base64
- type Client
- type FaultError
- type Option
- type Responsedeprecated
- func (r Response) Err() errordeprecated
- func (r Response) Unmarshal(v any) errordeprecated
- type TypeMismatchError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func EncodeMethodCall ¶
EncodeMethodCall encodes an XML-RPC method call with the given method name and arguments into XML bytes.
Example ¶
package main
import (
"fmt"
"log"
"github.com/ninech/xmlrpc"
)
func main() {
data, err := xmlrpc.EncodeMethodCall("Math.add", 1, 2)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
Output: <?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>Math.add</methodName><params><param><value><int>1</int></value></param><param><value><int>2</int></value></param></params></methodCall>
func NewRequest ¶
NewRequest creates an http.Request for an XML-RPC call to the given URL. The method parameter is the XML-RPC method name, and args contains the arguments to pass to the remote method.
func NewRequestContext ¶
func NewRequestContext( ctx context.Context, url string, method string, args any, ) (*http.Request, error)
NewRequestContext creates an http.Request with context for an XML-RPC call to the given URL. The method parameter is the XML-RPC method name, and args contains the arguments to pass to the remote method.
Types ¶
type Base64 ¶
type Base64 string
Base64 is a string type that will be encoded as base64 in XML-RPC requests.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents an XML-RPC client.
func NewClient
deprecated
func NewClient(requrl string, transport http.RoundTripper) (*Client, error)
NewClient creates a new XML-RPC client for the given URL. The transport parameter specifies the http.RoundTripper to use for HTTP requests. If transport is nil, http.DefaultTransport is used.
Deprecated: Use NewClientWithOptions instead.
func NewClientWithOptions ¶
NewClientWithOptions creates a new XML-RPC client for the given URL with the specified options.
func (*Client) Call ¶
Call invokes the named method, waits for it to complete, and returns its error status. This is equivalent to CallContext with context.Background.
func (*Client) CallContext ¶
CallContext invokes the named method with context support. The context controls cancellation and timeout of the HTTP request.
Example ¶
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/ninech/xmlrpc"
)
func main() {
client, err := xmlrpc.NewClientWithOptions("https://example.com/xmlrpc")
if err != nil {
log.Fatal(err)
}
defer func() { _ = client.Close() }()
// Create a context with a 5-second timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var result struct {
Status string `xmlrpc:"status"`
}
if err := client.CallContext(ctx, "App.status", nil, &result); err != nil {
log.Fatal(err)
}
fmt.Printf("Status: %s\n", result.Status)
}
type FaultError ¶
FaultError represents an XML-RPC fault response from the server.
func (FaultError) Error ¶
func (e FaultError) Error() string
Error returns the string representation of the fault.
type Option ¶
type Option func(*clientOptions)
Option configures a Client.
func WithBasicAuth ¶
WithBasicAuth sets basic authentication for all requests.
Example ¶
package main
import (
"log"
"github.com/ninech/xmlrpc"
)
func main() {
client, err := xmlrpc.NewClientWithOptions("https://example.com/xmlrpc",
xmlrpc.WithBasicAuth("username", "password"),
)
if err != nil {
log.Fatal(err)
}
defer func() { _ = client.Close() }()
var result string
if err := client.Call("Secure.method", nil, &result); err != nil {
log.Fatal(err)
}
}
func WithCookieJar ¶
WithCookieJar sets the cookie jar for the client. Pass nil to disable cookie handling.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client to use for requests. This takes precedence over WithTransport.
func WithHeader ¶
WithHeader adds a header to all requests. Can be called multiple times to add multiple headers.
func WithTransport ¶
func WithTransport(transport http.RoundTripper) Option
WithTransport sets the HTTP transport for requests. Ignored if WithHTTPClient is also used.
type Response
deprecated
type Response []byte
Response represents a raw XML-RPC response body.
Deprecated: Response is no longer used internally. Use Client.Call or Client.CallContext instead.
func (Response) Err
deprecated
Err checks if the response contains a fault and returns it as a FaultError. If the response is not a fault, Err returns nil.
Deprecated: Use Client.Call or Client.CallContext instead, which return FaultError directly.
func (Response) Unmarshal
deprecated
Unmarshal decodes the XML-RPC response into v.
Deprecated: Use Client.Call or Client.CallContext instead.
type TypeMismatchError ¶
type TypeMismatchError string
TypeMismatchError is returned when the XML-RPC response type does not match the expected Go type during unmarshaling.
func (TypeMismatchError) Error ¶
func (e TypeMismatchError) Error() string
Error returns the error message describing the type mismatch.