Skip to content

Instantly share code, notes, and snippets.

@novalagung
Last active April 20, 2023 19:16
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save novalagung/7ccd5a6a217b35ff4efb3af8e8a54752 to your computer and use it in GitHub Desktop.
Save novalagung/7ccd5a6a217b35ff4efb3af8e8a54752 to your computer and use it in GitHub Desktop.
Example implementation of making SOAP call on WSDL web service using go with only net/http package. The full tutorial avaiable on https://medium.com/eaciit-engineering/soap-wsdl-request-in-go-language-3861cfb5949e
package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/xml"
"fmt"
"log"
"net/http"
"strings"
)
type UserList struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
ListUsersResponse struct {
XMLName xml.Name
Return []string `xml:"return"`
} `xml:"listUsersResponse"`
}
}
func main() {
// wsdl service url
url := fmt.Sprintf("%s%s%s",
"https://12.34.56.78:9443",
"/services/RemoteUserStoreManagerService",
".RemoteUserStoreManagerServiceHttpsSoap11Endpoint",
)
// payload
payload := []byte(strings.TrimSpace(`
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.um.carbon.wso2.org">
<soapenv:Body>
<ser:listUsers>
<ser:filter></ser:filter>
<ser:maxItemLimit>100</ser:maxItemLimit>
</ser:listUsers>
</soapenv:Body>
</soapenv:Envelope>`,
))
httpMethod := "POST"
// soap action
soapAction := "urn:listUsers"
// authorization credentials
username := "admin"
password := "admin"
log.Println("-> Preparing the request")
// prepare the request
req, err := http.NewRequest(httpMethod, url, bytes.NewReader(payload))
if err != nil {
log.Fatal("Error on creating request object. ", err.Error())
return
}
// set the content type header, as well as the oter required headers
req.Header.Set("Content-type", "text/xml")
req.Header.Set("SOAPAction", soapAction)
req.SetBasicAuth(username, password)
// prepare the client request
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
log.Println("-> Dispatching the request")
// dispatch the request
res, err := client.Do(req)
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
return
}
log.Println("-> Retrieving and parsing the response")
// read and parse the response body
result := new(UserList)
err = xml.NewDecoder(res.Body).Decode(result)
if err != nil {
log.Fatal("Error on unmarshaling xml. ", err.Error())
return
}
log.Println("-> Everything is good, printing users data")
// print the users data
users := result.Body.ListUsersResponse.Return
fmt.Println(strings.Join(users, ", "))
}
@viseth
Copy link

viseth commented Oct 2, 2019

@novalagung, does it handle a payload with a soap:Header tag ?

	payload := []byte(strings.TrimSpace(`
	<?xml version="1.0" encoding="UTF-8"?>
	<soap:Envelope 
		xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
		xmlns:ns="http://lavasoft.com/"
		xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
		xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
		
	    **<soap:Header> 
		    <wsa:Action>'http://lavasoft.com/GetIpLocation'</wsa:Action>
	    </soap:Header>**		

		<soap:Body>
			<ns:GetIpLocation>
				<ns:sIp>8.8.8.8ss</ns:sIp>
			</ns:GetIpLocation>
		</soap:Body>
	</soap:Envelope>`,
	))

Actually when I set the tag soap:Header, it fails, assuming that tag is required in my payload SOAP webservice.

Thanks,
Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment