make a http options request in go

Here's an example of how to make an HTTP OPTIONS request in Go:

main.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    url := "https://example.com" // URL to make the request to

    // Create an OPTIONS request
    req, err := http.NewRequest("OPTIONS", url, nil)
    if err != nil {
        // Handle error
        fmt.Println("Error creating request:", err)
        return
    }

    // Send the OPTIONS request
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // Handle error
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()

    // Process the response
    fmt.Println("Response status:", resp.Status)
    fmt.Println("Response headers:", resp.Header)
}
700 chars
33 lines

In this example, we first create an http.NewRequest object with the method set to "OPTIONS" and the URL set to the desired endpoint. We then use an http.Client to send the request and obtain an http.Response object. Finally, we can process the response as needed (in this case, simply printing out the status and headers).

related categories

gistlibby LogSnag