Convert curl to Go net/http code
This curl to Go converter turns a command copied from API docs, a terminal or your browser's "Copy as cURL" menu into a complete package main program. The output uses only the standard library: net/http for the request, io to read the response, and strings, bytes or mime/multipart only when the request has a body. os is imported only for -T uploads and @file form fields.
Example
The default sample posts a JSON order with a bearer token and a 30-second --max-time. The core of the generated program looks like this:
client := &http.Client{Timeout: 30 * time.Second}
var data = strings.NewReader(`{"customer_id":42,"items":[{"sku":"MUG-01","qty":2}],"gift":false}`)
req, err := http.NewRequest("POST", "https://api.example.com/v1/orders?notify=true", data)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_API_TOKEN")
resp, err := client.Do(req)The JSON body is kept as a Go raw string literal (backticks), so quotes inside it need no escaping. If the body itself contains a backtick, the converter switches to a normal double-quoted string.
How curl flags map to Go
-X/--requestbecomes the first argument ofhttp.NewRequest. Without it, the method is inferred the way curl does: POST when there is a body, HEAD for-I, otherwise GET.-H,-A(User-Agent),-e(Referer) and-b name=valuebecomereq.Header.Setcalls; when a header is repeated, the later values usereq.Header.Addso none is overwritten.-d,--data-raw,--data-binary,--data-urlencodeand--jsonbecome astrings.NewReaderbody. A plain-dwithout a Content-Type header getsapplication/x-www-form-urlencoded, just like curl.-Fbuilds amultipart.Writer;@filefields are read withos.ReadFileand the boundary header comes fromwriter.FormDataContentType().-T filebecomes a PUT request whose body is the file opened withos.Open.-u user:passbecomesreq.SetBasicAuth.-m/--max-timesetshttp.Client{Timeout: ...}.-kadds anhttp.TransportwithInsecureSkipVerify: true. Use it only against test servers.-Gmoves the data into the query string.
Limitations
Go's http.Client follows up to 10 redirects by default, so the program follows redirects even when your command had no -L. Set a CheckRedirect function if you need curl's default behavior. Client certificates (--cert), proxy settings (-x) and --connect-timeout are not converted and are flagged with a comment, while retries are dropped without one. --compressed needs no code because Go's transport requests and decodes gzip automatically.
A -d @payload.json argument is replaced by a placeholder string plus a comment, since the tool cannot read files from your disk. The generated code also uses panic for errors to stay short; swap in proper error handling for production code.
Privacy
The conversion runs entirely in your browser. Bearer tokens, API keys and passwords in your command never leave the page. Need the same request in another language? Try curl to Python or curl to Rust. To type the response JSON, paste it into JSON to Go.