2017-07-21 4 views
1

を送信することは、私は、次の行くコードは、XMLは、次のHTTP POST golangに

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"` 
} 

と値を挿入するために、次のコードを使用しているこのXMLを作るために

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

を形成するために私の予想されるREQのxmlですそれ

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

が、ここでは形成され、要求が、私が形成されることが予想されていないように形成され 要求XMLは次のとおりです。{{} {12321321} {サンプルwww.//sample.com}}

ここで間違っているものを提案して下さい
+0

一つの問題は、XMLNameのはDocumentxmlとDELETEXMLで空です。空の '{}'値のためです。 –

+0

代わりに何を言及すべきか –

答えて

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> 

EDIT:

は、あなたのサーバが、ヘッダーとXMLを期待しているかもしれませ。同様below-

<?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