Go言語でXMLRPCクライアント

関連 : Go言語でXMLRPCサーバーのメモ - brainstorm

昨日書いてみたXMLRPCのサーバーにgolangからアクセスするコード

package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/divan/gorilla-xmlrpc/xml"
)

type TimeArgs struct {
}

type TimeReply struct {
	Time time.Time
}

func XmlRpcCall(method string, args TimeArgs) (reply TimeReply, err error) {
	buf, _ := xml.EncodeClientRequest(method, &args)
	body := bytes.NewBuffer(buf)

	resp, err := http.Post("http://localhost:8000/", "text/xml", body)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	xml.DecodeClientResponse(resp.Body, &reply)
	return
}

func main() {
	args := TimeArgs{}
	var reply TimeReply

	reply, err := XmlRpcCall("TimeService.Now", args)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Response: %s \n", reply.Time)
}