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

Output language

Converted in your browser — tokens and passwords in the command never leave your device. Tip: in Chrome DevTools, right-click a request in the Network tab → Copy → Copy as cURL.

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.

  1. The body is a strings.NewReader passed to http.NewRequest. JSON goes in a raw string literal, so quotes don’t need escaping.
  2. Go never sets Content-Type for you, so the code always sets it with req.Header.Set.
  3. -u user:pass becomes req.SetBasicAuth(user, pass).
  4. -F uses a mime/multipart writer. Files are added by a small addFile helper, and the Content-Type comes from mw.FormDataContentType() so the boundary matches.
  5. --max-time becomes the client’s Timeout. Go’s default client never times out, so the code uses 30 seconds when the command has no limit.
  6. -k becomes InsecureSkipVerify: true and -x becomes http.ProxyURL, both on the client’s Transport.
  7. Go follows up to 10 redirects by default. Unless the command has -L, the code adds a CheckRedirect that 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 command
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 10
Go
package 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. 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. 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. 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. 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).

Popular uses

Read the guide