Skip to content

Instantly share code, notes, and snippets.

@RichardKnop
Created August 12, 2016 09:11
Show Gist options
  • Save RichardKnop/8a1c513ded7effbb817ec179b5760aa0 to your computer and use it in GitHub Desktop.
Save RichardKnop/8a1c513ded7effbb817ec179b5760aa0 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/xml"
"fmt"
)
type getFileRequest struct {
XMLName struct{} `xml:"http://tempuri.org/ getFile"`
User string `xml:"userName"`
Password string `xml:"passWord"`
Filename string `xml:"FileName"`
}
type getFileResponse struct {
XMLName struct{} `xml:"http://tempuri.org/ getFileResponse"`
Result string `xml:"getFileResult"`
}
type SoapEnvelope struct {
XMLName struct{} `xml:"http://www.w3.org/2003/05/soap-envelope Envelope"`
Body SoapBody
}
type SoapBody struct {
XMLName struct{} `xml:"http://www.w3.org/2003/05/soap-envelope Body"`
Contents []byte `xml:",innerxml"`
}
const response = `<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getFileResponse xmlns="http://tempuri.org/">
<getFileResult>fileContentsinHexFormat</getFileResult> //file contents in hex format
</getFileResponse>
</soap12:Body>
</soap12:Envelope>
`
func SoapEncode(contents interface{}) ([]byte, error) {
data, err := xml.MarshalIndent(contents, " ", " ")
if err != nil {
return nil, err
}
data = append([]byte("\n"), data...)
env := SoapEnvelope{Body: SoapBody{Contents: data}}
return xml.MarshalIndent(&env, "", " ")
}
func SoapDecode(data []byte, contents interface{}) error {
env := SoapEnvelope{Body: SoapBody{}}
err := xml.Unmarshal(data, &env)
if err != nil {
return err
}
return xml.Unmarshal(env.Body.Contents, contents)
}
func main() {
req := &getFileRequest{User: "myusername", Password: "mypassword", Filename: "myfilename"}
data, err := SoapEncode(&req)
if err != nil {
panic(err)
}
fmt.Println("Request:")
fmt.Println(xml.Header + string(data))
var resp getFileResponse
err = SoapDecode([]byte(response), &resp)
if err != nil {
panic(err)
}
fmt.Println("Response:")
fmt.Println(resp.Result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment