Skip to content
ZeroServer.tools

curl to Go

Convert curl commands to Go net/http code. Supports headers, POST data, basic auth, and custom HTTP clients.

Presets
Go Code Options
48 B | 1 lines
HTTP MethodGET
Headers Detected0
Output size432 B (28 lines)
StatusReady

curl to Go net/http

Go's standard library net/http package provides a full-featured HTTP client. This converter maps curl flags to idiomatic Go: -H becomes req.Header.Set(), -d maps to a strings.NewReader payload, and -u becomes req.SetBasicAuth(). All processing runs entirely in your browser.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Converting an API's curl example into net/http for a Go service.
  • Reproducing a captured request inside an existing Go client.
  • Getting the request construction and header setup right.
  • Producing a snippet for a one-off command-line tool.
  • Checking how a body is expressed as an io.Reader.

Frequently Asked Questions

Why must I close the response body?
defer resp.Body.Close() is mandatory. Go does not close it for you, and an unclosed body leaks the underlying connection so it never returns to the pool — under load this exhausts file descriptors. This is the most common bug in ported Go HTTP code.
Does http.Client have a default timeout?
No. The zero value means NO timeout, so a hung server blocks forever. Set client := &http.Client{Timeout: 10 * time.Second}, or use a context with context.WithTimeout for per-request control.
How do I send a JSON body?
Marshal to bytes and wrap in a reader: bytes.NewBuffer(payload) passed to http.NewRequest, then set req.Header.Set("Content-Type", "application/json"). Go will not infer the content type from the value.
How does curl's -k translate?
A custom transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}. Note this disables verification for every request made through that client, not just one — build a separate client if you only need it for one host.
Does Go follow redirects automatically?
Yes, up to 10 by default — closer to curl -L than to bare curl. Override with the client's CheckRedirect func; returning http.ErrUseLastResponse stops following and hands you the 3xx directly.
How do I cancel a request?
With a context — `http.NewRequestWithContext` binds the request's lifetime to it, so cancelling the context aborts the in-flight call. That is the idiomatic mechanism; a client timeout is a blunter instrument covering the whole exchange.
Why reuse the Transport rather than the Client?
Because the `Transport` holds the connection pool. Creating a client per request with a fresh transport defeats keep-alive and leaks file descriptors under load — the default `http.DefaultTransport` is safe for concurrent use by design.

Common errors and gotchas

  • Not closing `resp.Body`, which leaks connections — the deferred close is not optional.
  • Using `http.DefaultClient`, which has no timeout at all and will hang forever.
  • Setting a content-type after creating the request rather than on the request's header map.
  • Assuming a non-2xx returns an error, when only a transport failure does.
  • Reusing a request across retries, whose body has already been consumed.

Related Developer Utilities tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate