Skip to content

Instantly share code, notes, and snippets.

@buroz
Last active April 2, 2024 09:33
Show Gist options
  • Save buroz/d4cc4b8016e5c21817b6704e93d54023 to your computer and use it in GitHub Desktop.
Save buroz/d4cc4b8016e5c21817b6704e93d54023 to your computer and use it in GitHub Desktop.
Golang SOAP Request Example
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type SoapHeader struct {
XMLName xml.Name `xml:"x:Header"`
}
type SoapBody struct {
XMLName xml.Name `xml:"x:Body"`
Request interface{}
}
type SoapRoot struct {
XMLName xml.Name `xml:"x:Envelope"`
X string `xml:"xmlns:x,attr"`
Sch string `xml:"xmlns:sch,attr"`
Header SoapHeader
Body SoapBody
}
type GetCitiesRequest struct {
XMLName xml.Name `xml:"sch:GetCitiesRequest"`
}
type GetCitiesResponse struct {
XMLName xml.Name `xml:"ns3:GetCitiesResponse"`
result struct{} `xml:result`
cities struct{} `xml:cities`
}
func SoapCall(service string, request interface{}) string {
var root = SoapRoot{}
root.X = "http://schemas.xmlsoap.org/soap/envelope/"
root.Sch = "http://www.n11.com/ws/schemas"
root.Header = SoapHeader{}
root.Body = SoapBody{}
root.Body.Request = request
out, _ := xml.MarshalIndent(&root, " ", " ")
body := string(out)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
response, err := client.Post(service, "text/xml", bytes.NewBufferString(body))
if err != nil {
fmt.Println(err)
}
defer response.Body.Close()
content, _ := ioutil.ReadAll(response.Body)
s := strings.TrimSpace(string(content))
return s
}
func main() {
SoapCall("https://api.n11.com/ws/CityService.wsdl", GetCitiesRequest{})
}
@ironytr
Copy link

ironytr commented Mar 31, 2021

Merhaba, gelen response'u nasıl parse edebiliriz?

@buroz
Copy link
Author

buroz commented Apr 5, 2021

Merhaba, gelen response'u nasıl parse edebiliriz?

buroz/gon11 burada çalışan bir örnek bulunmakta onun üzerinden gidersen daha rahat edebilirsin. Yine yardım gerekirse seve seve ederim mail atman yeterli.

@examitymadhu
Copy link

getting Authentication failed

@buroz
Copy link
Author

buroz commented Nov 3, 2021

getting Authentication failed

You have to pass username and password inside the body

@igorcavalcanti
Copy link

Thank for the insight. Helped a lot.

@Morty-debug
Copy link

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