make a http request with a custom method in go

To make a HTTP request with a custom method in Go, you can use the http.NewRequest() function along with the method name you want to use. Here's an example:

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

func main() {
    // Create a new request with custom method
    req, err := http.NewRequest("CUSTOM", "https://example.com", strings.NewReader("data"))
    if err != nil {
        // Handle error
    }

    // Make the request
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // Handle error
    }

    // Handle response
    defer resp.Body.Close()
    // ...
}
445 chars
24 lines

In the example above, we create a new http.Request with the method name "CUSTOM" and the URL "https://example.com". We also provide a request body using a strings.Reader.

Next, we use the http.Client to make the request and retrieve the response. Finally, we close the response body to prevent resource leaks.

You can replace "CUSTOM" with any other HTTP method name you want to use, such as "PATCH", "PUT", or "DELETE".

gistlibby LogSnag