将 XML 转换为 Golang 中的 HTTP POST 请求发送。

huangapple go评论72阅读模式
英文:

making xml to send in http post golang

问题

以下是我翻译好的内容:

以下是我期望生成的请求XML:

<custom key="234234e324e4">
    <document name="sample" location="http://example.com">
    </document>
</custom>

为了生成这个XML,我使用了以下Go代码:

type Documentxml struct {
    XMLName  xml.Name `xml:"document"`
    Name     string   `xml:"name,attr"`
    Location string   `xml:"location,attr"`
}

type DeleteXml struct {
    XMLName  xml.Name    `xml:"custom"`
    Key      string      `xml:"key,attr"`
    Document Documentxml `xml:"document"`
}

以及以下代码来插入值:

var requestxml DeleteXml
requestxml.Key = "12321321"
requestxml.Document.Name = "sample"
requestxml.Document.Location = "www.//sample.com"
bytexml, err := xml.Marshal(&requestxml)
client := &http.Client{}
url := "http://localhost:8080/searchblox/api/rest/docdelete"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bytexml))
if err != nil {
    fmt.Println(err)
}
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
    fmt.Println(err)
}
fmt.Println(resp)

但是生成的请求并不符合我的预期,请求XML的形式为:{{ } 12321321{{ } sample www.//sample.com}}。请指出问题出在哪里。

英文:

following is my expected req xml to be formed

&lt;custom key=&quot;234234e324e4&quot;&gt;
&lt;document name=&quot;sample&quot; location=&quot;http://example.com&quot;&gt;
&lt;/document&gt;
&lt;/custom&gt;

to make this xml i have used following go code

 type Documentxml struct {
	XMLName  xml.Name `xml:&quot;document&quot;`
	Name  string   `xml:&quot;name,attr&quot;`
	Location string   `xml:&quot;location,attr&quot;`
}
type DeleteXml struct {
	XMLName  xml.Name    `xml:&quot;custom&quot;`
	Key   string      `xml:&quot;key,attr&quot;`
	Document Documentxml `xml:&quot;document&quot;`
}

and following code to insert values into it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

var requestxml DeleteXml
    requestxml.Key = &quot;12321321&quot;
    requestxml.Document.Name = &quot;sample&quot;
    requestxml.Document.Location = &quot;www.//sample.com&quot;
bytexml, err := xml.Marshal(&amp;requestxml)
	client := &amp;http.Client{}
	url := &quot;http://localhost:8080/searchblox/api/rest/docdelete&quot;
	// build a new request, but not doing the POST yet
	req, err := http.NewRequest(&quot;POST&quot;, url, bytes.NewBuffer(bytexml))
	if err != nil {
		fmt.Println(err)
	}
req.Header.Add(&quot;Content-Type&quot;, &quot;application/xml; charset=utf-8&quot;)
	// now POST it
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(resp)

<!-- end snippet -->

but here request formed is not as i expected to be formed
request xml formed is: {{ } 12321321{{ } sample www.//sample.com}}
please suggest what is going wrong here

答案1

得分: 1

你的XML定义是正确的,并且你得到了预期的格式。然而,在你的问题中,字段requestxml.Document.Location的URL格式值不正确,不确定这是否可能是你的服务器的问题。

播放链接:https://play.golang.org/p/oCkteDAVgZ

输出:

&lt;custom key=&quot;12321321&quot;&gt;
  &lt;document name=&quot;sample&quot; location=&quot;http://www.sample.com&quot;&gt;&lt;/document&gt;
&lt;/custom&gt;

编辑:

也许你的服务器需要带有头部的XML。像下面这样-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;custom key=&quot;12321321&quot;&gt;
  &lt;document name=&quot;sample&quot; location=&quot;http://www.sample.com&quot;&gt;&lt;/document&gt;
&lt;/custom&gt;

带有头部的更新代码,播放链接:https://play.golang.org/p/n4VYXxLE6R

英文:

Your XML definition is correct and you're getting expected format. However in your question. Field requestxml.Document.Location has incorrect URL format value, not sure if this could be problem as per your server.

Play Link: https://play.golang.org/p/oCkteDAVgZ

Output:

&lt;custom key=&quot;12321321&quot;&gt;
  &lt;document name=&quot;sample&quot; location=&quot;http://www.sample.com&quot;&gt;&lt;/document&gt;
&lt;/custom&gt;

EDIT:

May be your server is expecting XML with header. Like below-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;custom key=&quot;12321321&quot;&gt;
  &lt;document name=&quot;sample&quot; location=&quot;http://www.sample.com&quot;&gt;&lt;/document&gt;
&lt;/custom&gt;

Your update code with header, play link: https://play.golang.org/p/n4VYXxLE6R

huangapple
  • 本文由 发表于 2017年7月21日 16:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/45232693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定