make a http put request in go

Here's an example code snippet showing how to make an HTTP PUT request in Go using the standard net/http package:

main.go
import (
	"bytes"
	"net/http"
)

func main() {
	url := "https://example.com/api/some-resource/123"

	// create a new buffer with the JSON content to send in the request body
	requestBody := bytes.NewBuffer([]byte(`{"name": "updated resource name"}`))

	// create a new HTTP PUT request with the desired URL and request body
	request, err := http.NewRequest("PUT", url, requestBody)
	if err != nil {
		// handle error
	}

	// set the request Content-Type header to indicate the type of the request body
	request.Header.Set("Content-Type", "application/json")

	// make the HTTP PUT request using the default client
	client := http.DefaultClient
	response, err := client.Do(request)
	if err != nil {
		// handle error
	}

	defer response.Body.Close()

	// handle response here
}
777 chars
32 lines

This code creates a new http.Request object with the desired HTTP method, URL, and request body. It then sets the Content-Type header to indicate the type of the request body, and makes the request using the default HTTP client. The response is handled using the http.Response object that is returned.

gistlibby LogSnag