如何在gowsdl上添加AuthHeader

问题描述

我正在将肥皂服务与使用gowsdl库集成。

我无法创建这样的auth标头;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header>
      <tem:AuthHeader>
         <!--Optional:-->
         <tem:UserName>?</tem:UserName>
         <!--Optional:-->
         <tem:Password>?</tem:Password>
      </tem:AuthHeader>
   </soapenv:Header>
   <soapenv:Body>
   </soapenv:Body>
</soapenv:Envelope>

我正在使用gowsdl生成代码

我将稍后解释;

service.go中的AuthHeader

type AuthHeader struct {
    UserName string `xml:"UserName,omitempty" json:"UserName,omitempty"`

    Password string `xml:"Password,omitempty" json:"Password,omitempty"`
}

我正在这样使用

authHeader := service.AuthHeader{
    Password: Password,UserName: Username,}

header := struct {
    AuthHeader service.AuthHeader `xml:"AuthHeader,omitempty" json:"AuthHeader,omitempty"`
}{
    AuthHeader:authHeader,}

添加这样的标题

soapClient := soap.NewClient(Url)

soapClient.AddHeader(header)

但是它不起作用!我该如何解决这个问题?

解决方法

为什么从gowsdl包的example.go中像这样设置auth标头:

client := soap.NewClient(
        Url,soap.WithBasicAuth("usr","psw"),)