Home >Backend Development >Golang >How Can I Handle SOAP Requests and Responses in Go Without Direct WSDL Support?

How Can I Handle SOAP Requests and Responses in Go Without Direct WSDL Support?

Barbara Streisand
Barbara StreisandOriginal
2024-12-02 01:00:14712browse

How Can I Handle SOAP Requests and Responses in Go Without Direct WSDL Support?

WSDL/SOAP Support in Go

While there is no direct support for WSDL in Go, there are several options available for dealing with SOAP requests and responses.

Building SOAP Requests Manually

The standard encoding/xml package can be used to create SOAP requests manually, but it lacks the functionality necessary to handle certain aspects of SOAP, such as namespaces and prefixes.

Using xmlutil Library

The github.com/webconnex/xmlutil library provides a more robust solution for handling SOAP requests and responses. It includes features such as:

  • Namespace and prefix registry
  • Custom type registration
  • XML encoding and decoding with type information
  • Support for type enforcement, xsi:type attributes, and more

Manually Decoding SOAP Responses

To decode SOAP responses, you can use the encoding/xml package or libraries like xmlutil. The process involves using the decoder to parse the XML response and extract the desired data.

Example Using xmlutil

Here is a modified version of the example provided in the answer:

package main

import (
    "bytes"
    "github.com/webconnex/xmlutil"
    "log"
    "strings"
    "encoding/xml"
)

type Envelope struct {
    Body `xml:"soap:"`
}

type Body struct {
    Msg interface{}
}

type MethodCall struct {
    One string
    Two string
}

type MethodCallResponse struct {
    Three string
}

func main() {
    x := xmlutil.NewXmlUtil()
    x.RegisterNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi")
    x.RegisterNamespace("http://www.w3.org/2001/XMLSchema", "xsd")
    x.RegisterNamespace("http://www.w3.org/2003/05/soap-envelope", "soap")
    x.RegisterTypeMore(Envelope{}, xml.Name{"http://www.w3.org/2003/05/soap-envelope", ""},
        []xml.Attr{
            xml.Attr{xml.Name{"xmlns", "xsi"}, "http://www.w3.org/2001/XMLSchema-instance"},
            xml.Attr{xml.Name{"xmlns", "xsd"}, "http://www.w3.org/2001/XMLSchema"},
            xml.Attr{xml.Name{"xmlns", "soap"}, "http://www.w3.org/2003/05/soap-envelope"},
        })
    x.RegisterTypeMore("", xml.Name{}, []xml.Attr{
        xml.Attr{xml.Name{"http://www.w3.org/2001/XMLSchema-instance", "type"}, "xsd:string"},
    })

    buf := new(bytes.Buffer)
    buf.WriteString(`<soap:Envelope><soap:Body><MethodCall><One>one</One><Two>two</Two></MethodCall></soap:Body></soap:Envelope>`)

    enc := x.NewEncoder(buf)
    env := &Envelope{Body{MethodCall{
        One: "one",
        Two: "two",
    }}}
    if err := enc.Encode(env); err != nil {
        log.Fatal(err)
    }

    response := `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <MethodCallResponse>
            <Three>three</Three>
        </MethodCallResponse>
    </soap:Body>
</soap:Envelope>`
    
    dec := x.NewDecoder(strings.NewReader(response))
    var resp Envelope
    if err := dec.DecodeElement(&resp, nil); err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%#v\n", resp)
}

The above is the detailed content of How Can I Handle SOAP Requests and Responses in Go Without Direct WSDL Support?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn