Created
August 18, 2014 04:20
-
-
Save slav123/cbb3309052de5a870667 to your computer and use it in GitHub Desktop.
PUT request GO LANG
This file contains 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
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) | |
} | |
} |
Thank You man Very Use ful
Thank you! This was very helpful.
How can I make a put request without body?
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
Thank you! This was very helpful.