2

Hello I'm new at golang and programming and i have a noobish question. i couldn't find the answer on google. The soap server fails with generated code by gowsdl. but i add this xmlns="" to auth tag its works like a charm. So how can i add this to tags not by strings replace but go idiomatic way?

Not accepted by server


<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <GetCitiesRequest xmlns="http://www.n11.com/ws/schemas">
        <auth>                     <<<<<<<<<<<<<<<<------------ fails because no xmlns=""
            <appKey>xxx</appKey>
            <appSecret>xx</appSecret>
        </auth>
    </GetCitiesRequest>
    </Body>
</Envelope>

Accepted by server


 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <GetCitiesRequest xmlns="http://www.n11.com/ws/schemas">
            <auth xmlns="">
                <appKey>[string]</appKey>
                <appSecret>[string]</appSecret>
            </auth>
        </GetCitiesRequest>
    </Body>
</Envelope>

im using quick fix:

buffers := new(bytes.Buffer)
buffers.WriteString(strings.ReplaceAll(buffer.String(),"<auth>","<auth xmlns=\"\">"))

req, err := http.NewRequest("POST", s.url, buffers)

what should i add struct tag to see empty xmlns="" ?

type GetCitiesRequest struct {
    XMLName xml.Name `xml:"http://www.n11.com/ws/schemas GetCitiesRequest"`

    Auth *Authentication `xml:"auth,omitempty" json:"auth,omitempty"`
}
type Authentication struct {
    AppKey string `xml:"appKey,omitempty" json:"appKey,omitempty"`

    AppSecret string `xml:"appSecret,omitempty" json:"appSecret,omitempty"`
}

Alos i tried;

type Authentication struct {
   XMLName xml.Name `xml:""`

   AppKey string `xml:"appKey,omitempty" json:"appKey,omitempty"`

   AppSecret string `xml:"appSecret,omitempty" json:"appSecret,omitempty"`
}


   auth := Authentication{AppKey:"secret",AppSecret:"secret"}
   auth.XMLName.Local= "auth"
   auth.XMLName.Space = ""

Also i tried auth.XMLName.Space = " " empty space but xml.marshal transform it to escaped character like "&quote,#34"

I want to understand how can i do with like pro way but not noob way. Any help appreciated. Thank you.


another problem :/ not solved

tried xml:"* categoryId" turns out -> <categoryId xmlns="*">1001770</categoryId> yet soap api not accepting * character wants <categoryId xmlns="">1001770</categoryId>

type Authentication struct {
    Xmlns string    `xml:"xmlns,attr"  json:"-"`
    AppKey string `xml:"appKey,omitempty" json:"appKey,omitempty"`

    AppSecret string `xml:"appSecret,omitempty" json:"appSecret,omitempty"`
}

type GetSubCategoriesRequest struct {
    XMLName xml.Name `xml:"http://www.n11.com/ws/schemas GetSubCategoriesRequest"`

    Auth *Authentication `xml:"auth,omitempty" json:"auth,omitempty"`

    CategoryId int64 `xml:"* categoryId,omitempty" json:"categoryId,omitempty"` <<<<<<<-------- i need xmlns="" 
}

any help?

1 Answers1

2

https://golang.org/pkg/encoding/xml/#Marshal

  • a field with tag "name,attr" becomes an attribute with the given name in the XML element.
  • a field with tag ",attr" becomes an attribute with the field name in the XML element.
type Authentication struct {
    Xmlns     string `xml:"xmlns,attr" json:"-"`
    AppKey    string `xml:"appKey,omitempty" json:"appKey,omitempty"`
    AppSecret string `xml:"appSecret,omitempty" json:"appSecret,omitempty"`
}

https://play.golang.org/p/iIvlUoaYvgB

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • 1
    Thank you sir, i spend alot of time on net to find answer. Wish i before ask this question on stackoverflow. thank you, thank, you – nonameforme Apr 03 '21 at 06:29