dtmsaqtly798322992 2017-07-21 08:24
浏览 464

使xml发送到http post golang中

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

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)

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

</div>
  • 写回答

1条回答 默认 最新

  • duanlun4411 2017-07-21 08:47
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?