Created
May 19, 2012 19:28
-
-
Save mbeale/2732090 to your computer and use it in GitHub Desktop.
GO and XML sample4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Generic Reader | |
type nopCloser struct { | |
io.Reader | |
} | |
//update function | |
func (a *Account) Update() error { | |
if xmlstring, err := xml.MarshalIndent(a, "", " "); err == nil { | |
xmlstring = []byte(xml.Header + string(xmlstring)) | |
client := &http.Client{} | |
body := nopCloser{bytes.NewBufferString(string(xmlstring))} | |
if req, err := http.NewRequest("PUT", "http://example.com/someresource", body); err != nil { | |
return err | |
} else { | |
req.Header.Add("Accept", "application/xml") | |
req.Header.Add("Content-Type","application/xml; charset=utf-8") | |
req.ContentLength = int64(len(string(msgbody))) | |
if resp, resperr := client.Do(req); resperr != nil { | |
return resperr | |
} | |
} | |
} else { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
account, _ = GetAccount("account_code_here") | |
account.FirstName = "Santa" | |
account.LastName = "Claus" | |
if err := account.Update(); err == nil { | |
println("Successfully Updated") | |
} else { | |
println(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment