Skip to content

Instantly share code, notes, and snippets.

@slav123
Created August 18, 2014 04:20
Show Gist options
  • Save slav123/cbb3309052de5a870667 to your computer and use it in GitHub Desktop.
Save slav123/cbb3309052de5a870667 to your computer and use it in GitHub Desktop.
PUT request GO LANG
func doPut(url string) {
client := &http.Client{}
request, err := http.NewRequest("PUT", url, strings.NewReader("<golang>really</golang>"))
request.SetBasicAuth("admin", "admin")
request.ContentLength = 23
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println("The calculated length is:", len(string(contents)), "for the url:", url)
fmt.Println(" ", response.StatusCode)
hdr := response.Header
for key, value := range hdr {
fmt.Println(" ", key, ":", value)
}
fmt.Println(contents)
}
}
@andydonzelli
Copy link

Thank you! This was very helpful.

@MuthukumarHelios
Copy link

Thank You man Very Use ful

@NisalSP9
Copy link

Thank you! This was very helpful.

@g10guang
Copy link

g10guang commented Apr 1, 2019

How can I make a put request without body?

@parzibyte
Copy link

How can I make a put request without body?

Just pass nil to the third argument:

request, err := http.NewRequest("PUT", url, nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment