英文:
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
<custom key="234234e324e4">
<document name="sample" location="http://example.com">
</document>
</custom>
to make this xml i have used following go code
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"`
}
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 = "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"
// build a new request, but not doing the POST yet
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")
// 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
输出:
<custom key="12321321">
<document name="sample" location="http://www.sample.com"></document>
</custom>
编辑:
也许你的服务器需要带有头部的XML。像下面这样-
<?xml version="1.0" encoding="UTF-8"?>
<custom key="12321321">
<document name="sample" location="http://www.sample.com"></document>
</custom>
带有头部的更新代码,播放链接: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:
<custom key="12321321">
<document name="sample" location="http://www.sample.com"></document>
</custom>
EDIT:
May be your server is expecting XML with header. Like below-
<?xml version="1.0" encoding="UTF-8"?>
<custom key="12321321">
<document name="sample" location="http://www.sample.com"></document>
</custom>
Your update code with header, play link: https://play.golang.org/p/n4VYXxLE6R
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论