Convert cURL to Go (net/http)
Paste a curl command to get a complete Go program that uses only the standard library. Save it as main.go and run go run main.go.
AI DevRuns in your browser — nothing uploaded
How curl options map to Go
Go’s net/http does very little for you, so the converted code spells out the things curl does quietly.
- The body is a
strings.NewReaderpassed tohttp.NewRequest. JSON goes in a raw string literal, so quotes don’t need escaping. - Go never sets Content-Type for you, so the code always sets it with
req.Header.Set. -u user:passbecomesreq.SetBasicAuth(user, pass).-Fuses amime/multipartwriter. Files are added by a smalladdFilehelper, and the Content-Type comes frommw.FormDataContentType()so the boundary matches.--max-timebecomes the client’sTimeout. Go’s default client never times out, so the code uses 30 seconds when the command has no limit.-kbecomesInsecureSkipVerify: trueand-xbecomeshttp.ProxyURL, both on the client’sTransport.- Go follows up to 10 redirects by default. Unless the command has
-L, the code adds aCheckRedirectthat stops at the first response, like curl.
Always close the response body with defer resp.Body.Close(), or Go can’t reuse the connection. Like curl, Go doesn’t treat a 404 or 500 as an error — check resp.StatusCode.
Example: curl to Go
This command, pasted into the converter above, gives the code below.
curl -X POST https://api.example.com/v1/webhooks \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"url": "https://myapp.example/hooks/orders", "events": ["order.paid"]}' \
--max-time 10package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
func main() {
body := strings.NewReader(`{
"url": "https://myapp.example/hooks/orders",
"events": [
"order.paid"
]
}`)
req, err := http.NewRequest("POST", "https://api.example.com/v1/webhooks", body)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 10 * time.Second,
// curl doesn't follow redirects without -L. Delete this to follow them like most clients.
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.StatusCode)
fmt.Println(string(respBody))
}How it works
- 1
Reads the command like a shell
Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.
- 2
Understands curl’s options
-X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.
- 3
Writes idiomatic code
json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.
- 4
Tested against real curl
Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.
Questions, answered
No. The code only imports the standard library — net/http, mime/multipart, crypto/tls and so on — and runs on any recent Go version (1.16 or newer).
http.Get and http.Post can’t set custom headers such as Authorization. http.NewRequest plus client.Do works for every method and header, so the converter always uses it.
A string sends exactly what curl sent. In your own code you’ll usually define a struct and build the body with json.Marshal — the request itself stays the same.
Replace io.ReadAll with json.NewDecoder(resp.Body).Decode(&result), where result is a struct (or map[string]any) that matches the response.
Check the User-Agent: Go sends Go-http-client/1.1, and some APIs block unknown clients. Add the header from the original command with req.Header.Set("User-Agent", …).
Popular uses
Read the guide
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- Regex ExplainerPaste a regex, get plain English plus test cases.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev