### Install http2curl Go Package Source: https://github.com/moul/http2curl/blob/master/README.md Install the http2curl library using the go get command. ```bash go get moul.io/http2curl ``` -------------------------------- ### Convert Form-Encoded POST Request Source: https://context7.com/moul/http2curl/llms.txt This example demonstrates converting a form-encoded POST request. The library correctly handles URL-encoded body data and normalizes header casing, as shown with the 'API_KEY' header. ```go package main import ( "bytes" "fmt" "io/ioutil" "net/http" "net/url" "moul.io/http2curl/v2" ) func main() { // Build form-encoded body form := url.Values{} form.Add("age", "10") form.Add("name", "Hudson") body := form.Encode() req, _ := http.NewRequest(http.MethodPost, "http://foo.com/cats", ioutil.NopCloser(bytes.NewBufferString(body))) req.Header.Set("API_KEY", "123") command, _ := http2curl.GetCurlCommand(req) fmt.Println(command) // Output: curl -X 'POST' -d 'age=10&name=Hudson' -H 'Api_key: 123' 'http://foo.com/cats' --compressed } ``` -------------------------------- ### HTTPS Request Handling Source: https://context7.com/moul/http2curl/llms.txt Demonstrates how the library handles HTTPS requests by automatically including the `-k` flag for insecure connections. ```APIDOC ## PUT /abc/def.ghi (HTTPS) ### Description Converts an HTTPS request to a curl command. The library automatically adds the `-k` flag to allow insecure connections, which is useful for development and testing. ### Method PUT ### Endpoint /abc/def.ghi?jlk=mno&pqr=stu ### Parameters #### Query Parameters - **jlk** (string) - Optional - Description not specified - **pqr** (string) - Optional - Description not specified #### Request Body - **hello** (string) - Required - The value 'world' - **answer** (integer) - Required - The value 42 ### Request Example ```json { "hello": "world", "answer": 42 } ``` ### Response #### Success Response (200) - **command** (string) - The generated curl command string, including the `-k` flag for HTTPS. #### Response Example ```bash curl -k -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed ``` ``` -------------------------------- ### Log Incoming HTTP Requests as Curl Commands Source: https://context7.com/moul/http2curl/llms.txt Use this server-side within HTTP handlers to log incoming requests as curl commands. This is useful for debugging and reproducing client requests. ```go package main import ( "fmt" "net/http" "moul.io/http2curl/v2" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Convert incoming request to curl command for logging command, err := http2curl.GetCurlCommand(r) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // Log the curl command for debugging/replay fmt.Printf("Received request: %s\n", command.String()) // Continue with normal request handling w.Write([]byte("OK")) }) http.ListenAndServe(":8080", nil) } ``` -------------------------------- ### CurlCommand Type - Formatting Options Source: https://context7.com/moul/http2curl/llms.txt Illustrates how to use the `CurlCommand` type, which is a string slice, to format the generated curl command as a single line or a multi-line string. ```APIDOC ## CurlCommand Type Formatting ### Description Demonstrates how to format the `CurlCommand` (a string slice) for single-line or multi-line output. ### Method N/A (Illustrates usage of the `CurlCommand` type) ### Endpoint N/A ### Response #### Success Response (200) - **command_string** (string) - The formatted curl command string. #### Response Example (Single Line) ```bash curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed ``` #### Response Example (Multi-line) ```bash curl \ -X \ 'PUT' \ -d \ '{"hello":"world","answer":42}' \ -H \ 'Content-Type: application/json' \ 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' \ --compressed ``` ``` -------------------------------- ### GetCurlCommand - Basic Usage Source: https://context7.com/moul/http2curl/llms.txt Demonstrates the primary function `GetCurlCommand` to convert a standard HTTP request with headers and a JSON body into a curl command. ```APIDOC ## PUT /abc/def.ghi ### Description Converts an http.Request object into a curl command string. This example shows a PUT request with a JSON body and custom headers. ### Method PUT ### Endpoint /abc/def.ghi?jlk=mno&pqr=stu ### Parameters #### Query Parameters - **jlk** (string) - Optional - Description not specified - **pqr** (string) - Optional - Description not specified #### Request Body - **hello** (string) - Required - The value 'world' - **answer** (integer) - Required - The value 42 ### Request Example ```json { "hello": "world", "answer": 42 } ``` ### Response #### Success Response (200) - **command** (string) - The generated curl command string. #### Response Example ```bash curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed ``` ``` -------------------------------- ### Convert HTTPS Request with Insecure Flag Source: https://context7.com/moul/http2curl/llms.txt For HTTPS URLs, the library automatically includes the `-k` flag to bypass certificate verification. This is useful for development and testing environments where certificate validation might be an issue. ```go package main import ( "bytes" "fmt" "net/http" "moul.io/http2curl/v2" ) func main() { payload := bytes.NewBufferString(`{"hello":"world","answer":42}`) req, _ := http.NewRequest("PUT", "https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", payload) req.Header.Set("X-Auth-Token", "private-token") req.Header.Set("Content-Type", "application/json") command, _ := http2curl.GetCurlCommand(req) fmt.Println(command) // Output: curl -k -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed } ``` -------------------------------- ### Convert http.Request to CURL Command Source: https://github.com/moul/http2curl/blob/master/README.md Use GetCurlCommand to convert an http.Request object to a cURL command string. Ensure necessary imports are included. ```go import ( "http" "moul.io/http2curl" ) data := bytes.NewBufferString(`{"hello":"world","answer":42}`) req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", data) req.Header.Set("Content-Type", "application/json") command, _ := http2curl.GetCurlCommand(req) fmt.Println(command) // Output: curl -X PUT -d "{\"hello\":\"world\",\"answer\":42}" -H "Content-Type: application/json" http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu ``` -------------------------------- ### Format CurlCommand as Multi-line Source: https://context7.com/moul/http2curl/llms.txt The CurlCommand type implements the Stringer interface for single-line output. It can also be joined with " \n " to produce a multi-line, more readable curl command for terminals. ```go package main import ( "bytes" "fmt" "net/http" "strings" "moul.io/http2curl/v2" ) func main() { req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`)) req.Header.Set("Content-Type", "application/json") command, _ := http2curl.GetCurlCommand(req) // Print as single line using String() method fmt.Println(command.String()) // Output: curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed // Print as multi-line for readability by joining with backslash-newline fmt.Println(strings.Join(*command, " \ ")) // Output: // curl \ // -X \ // 'PUT' \ // -d \ // '{"hello":"world","answer":42}' \ // -H \ // 'Content-Type: application/json' \ // 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' \ // --compressed } ``` -------------------------------- ### POST Form-Encoded Request Source: https://context7.com/moul/http2curl/llms.txt Shows how to convert a POST request with form-encoded data in the body into a curl command. ```APIDOC ## POST /cats ### Description Converts an HTTP POST request with URL-encoded form data in the body to a curl command. Handles header case normalization. ### Method POST ### Endpoint /cats ### Parameters #### Request Body - **age** (string) - Required - The age value, e.g., '10' - **name** (string) - Required - The name value, e.g., 'Hudson' ### Request Example ``` name=Hudson&age=10 ``` ### Response #### Success Response (200) - **command** (string) - The generated curl command string. #### Response Example ```bash curl -X 'POST' -d 'age=10&name=Hudson' -H 'Api_key: 123' 'http://foo.com/cats' --compressed ``` ``` -------------------------------- ### Handle Special Characters in Request Bodies Source: https://context7.com/moul/http2curl/llms.txt The library properly escapes special characters in request bodies, including single quotes, dollar signs, and other shell metacharacters, using bash-safe quoting. ```go package main import ( "bytes" "fmt" "net/http" "moul.io/http2curl/v2" ) func main() { // Body with special characters that need escaping body := bytes.NewBufferString(`Hello $123 o'neill -"-`) req, _ := http.NewRequest("POST", "http://www.example.com/api", body) req.Header.Set("Content-Type", "application/json") command, _ := http2curl.GetCurlCommand(req) fmt.Println(command) // Output: curl -X 'POST' -d 'Hello $123 o'\''neill -"-' -H 'Content-Type: application/json' 'http://www.example.com/api' --compressed } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.