cURL to Go: from curl to code
Paste a curl command and get a complete Go program that makes the same request with the standard library: http.NewRequest, headers with Header.Set, the body from strings.NewReader, and a client that returns redirects as they are, like curl does without -L. It uses only net/http, so there is nothing to install.
This page opens the converter on Go net/http. The command is only read and turned into text: nothing is run and nothing is sent from the page.
A real conversion
The tool opens with this command, a POST of a JSON order with a bearer token:
curl -X POST 'https://api.example.com/v1/orders?expand=items' \
-H 'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc' \
-H 'Content-Type: application/json' \
-d '{"customer":"ana@example.com","items":[{"sku":"KB-01","qty":2}],"coupon":null}'and this is what it writes for Go net/http:
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
body := strings.NewReader("{\"customer\":\"ana@example.com\",\"items\":[{\"sku\":\"KB-01\",\"qty\":2}],\"coupon\":null}")
req, err := http.NewRequest("POST", "https://api.example.com/v1/orders?expand=items", body)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
fmt.Println(res.Status)
fmt.Println(string(data))
}Which flags become what
Starting from a plain GET of https://api.example.com/items, each flag below adds this to the generated code (computed with the converter itself):
| Flag | What it is | First new line |
|---|---|---|
-H 'Accept: application/json' | a request header | req.Header.Set("Accept", "application/json") |
-d 'a=1&b=2' | a body (the method becomes POST) | "strings" |
-u ana:secret | basic authentication | req.SetBasicAuth("ana", "secret") |
-F 'photo=@a.png' | a multipart file upload | "bytes" |
-X DELETE | another method | req, err := http.NewRequest("DELETE", "https://api.example.com/items", nil) |
Another command
curl -X DELETE https://api.example.com/items/42 -H 'Authorization: Bearer abc'package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("DELETE", "https://api.example.com/items/42", nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer abc")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
fmt.Println(res.Status)
fmt.Println(string(data))
}No body, so the third argument of NewRequest is nil. The default client has no timeout: add one with http.Client{Timeout: …} for anything that reaches production.
Running it
go run main.go
# or, with a timeout instead of the zero-value client:
# client := &http.Client{Timeout: 10 * time.Second}Common mistakes
- Leaving the default client without a timeout: a server that never answers blocks the goroutine forever. Set Timeout on the client.
- Forgetting defer res.Body.Close(): the connection is not returned to the pool and file descriptors leak under load.
How to use the converter
- Copy the command: from the browser (network tab, Copy as cURL), from an API reference or from your terminal history.
- Paste it in the box; lines that end with a backslash are joined, so a multi-line command works.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert curl to Go?
Paste the command and copy the program; save it as main.go and run go run main.go. It prints the status and the body.
Why are quotes escaped in the body string?
The body is written as a Go interpreted string, so every double quote becomes \". For long JSON you can switch to a backtick raw string by hand.
Can I use it with a JSON struct?
Yes: generate the struct from the response with the JSON to Go page, then decode res.Body with json.NewDecoder(res.Body).Decode(&value).