cURL to Go Converter
Convert cURL commands to Go net/http — get a complete, runnable Go program instantly
You have a cURL command that makes an API request and need to replicate it in Go. Writing Go HTTP client code from scratch means setting up the client, building the request, attaching headers one by one, and handling every error. This converter generates a complete, runnable Go program from any cURL command — no boilerplate to write.
Why Go’s net/http?
Go’s standard library net/http package is production-grade and ships with every Go installation. It offers connection pooling, keep-alive, redirect handling, and TLS — all without any external dependencies. Unlike many other languages, Go does not need a third-party HTTP client library for most use cases. Code generated by this converter has zero external dependencies and compiles and runs with only the Go standard library.
What the Converter Generates
The output is a complete Go program with:
package maindeclaration- All necessary imports (
fmt,io,net/http, andstringsif a body is present) - A
main()function http.NewRequest()with the correct method and URLreq.Header.Set()calls for each headerreq.SetBasicAuth()for basic authenticationio.ReadAll()to read the response body- Idiomatic
if err != nil { panic(err) }error handling
Copy the output into a file named main.go and run it with go run main.go.
Mapping cURL Flags to Go
| cURL flag | Go net/http equivalent |
|---|---|
-X PUT | http.NewRequest("PUT", url, body) |
-H "Accept: application/json" | req.Header.Set("Accept", "application/json") |
-d '{"x":1}' | strings.NewReader("{\"x\":1}") as body |
-u user:pass | req.SetBasicAuth("user", "pass") |
-b "token=abc" | req.Header.Set("Cookie", "token=abc") |
-k / --insecure | custom http.Client with tls.Config{InsecureSkipVerify: true} |
Customizing the HTTP Client
The generated code uses the default http.Client{}. For production use, always configure timeouts to avoid hanging connections:
client := &http.Client{
Timeout: 10 * time.Second,
}
For TLS configuration (such as custom certificates or client certificates), create a custom http.Transport:
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false, // never true in production
},
}
client := &http.Client{Transport: transport}
Parsing JSON Responses in Go
The generated code reads the response body as a raw string with io.ReadAll. To parse a JSON response into a Go struct:
type APIResponse struct {
ID int `json:"id"`
Name string `json:"name"`
}
var result APIResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
panic(err)
}
fmt.Println(result.Name)
Import encoding/json to use json.NewDecoder.
Context and Cancellation
For long-running requests in production Go code, use context to support cancellation and deadlines:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
Replace http.NewRequest with http.NewRequestWithContext and pass the context as the first argument.
Frequently Asked Questions
Does the Go output work with Go modules?
Yes. Create a directory, run go mod init example.com/myapp, paste the generated code into main.go, and run go run main.go. No external modules are needed.
How do I send JSON and unmarshal the response in one step?
Add encoding/json to your imports. After reading the body with io.ReadAll, pass the bytes to json.Unmarshal(&data, &result) or use json.NewDecoder(resp.Body).Decode(&result) directly without io.ReadAll.
Why does Go panic instead of returning errors?
The generated code uses panic(err) as a simple, concise pattern for a standalone program. In production services, replace panics with proper error propagation using return fmt.Errorf("request failed: %w", err).