cURL to Go Code Converter

Convert cURL commands to Go HTTP code instantly. Get idiomatic net/http snippets with headers, authentication, request body, and TLS options preserved — ready to paste into your Go project.

Why Convert cURL to Go

Go's net/http package is verbose compared to one-line cURL commands. Converting a multi-header API request with auth and a JSON body by hand means writing 20+ lines of boilerplate: creating a client, building the request, setting headers one by one, reading and closing the body. This converter takes your cURL command copied from browser DevTools, Postman, or API docs and produces working Go code with proper error handling, header setting, and response reading — no manual translation required.

  • net/http output: Idiomatic Go using the standard library — no third-party dependencies
  • Headers preserved: All -H headers converted to req.Header.Set() calls
  • Auth support: Bearer tokens, Basic auth, and API keys translated correctly
  • Request body: JSON, form data, and raw body converted to strings.NewReader()
  • Error handling: Generated code includes proper Go error checks throughout

Choose the Right Variant

Step-by-Step Tutorial

  1. Copy your cURL command — from browser DevTools (right-click request → Copy as cURL), Postman, or API docs
  2. Paste it into the converter input: e.g. curl -X POST https://api.example.com/users -H "Authorization: Bearer token123" -H "Content-Type: application/json" -d '{"name":"Alice"}'
  3. Select Go as the target language
  4. The converter outputs a complete Go snippet with imports, request creation, headers, body, and response reading
  5. Copy the Go code and paste it into your project — it compiles and runs as-is

Example Conversion

Input cURL:

curl -X POST https://api.example.com/data \
  -H "Authorization: Bearer mytoken" \
  -H "Content-Type: application/json" \
  -d '{"key":"value"}'

Output Go (net/http):

package main

import (
    "fmt"
    "io"
    "net/http"
    "strings"
)

func main() {
    body := strings.NewReader(`{"key":"value"}`)
    req, _ := http.NewRequest("POST", "https://api.example.com/data", body)
    req.Header.Set("Authorization", "Bearer mytoken")
    req.Header.Set("Content-Type", "application/json")
    resp, err := http.DefaultClient.Do(req)
    if err != nil { panic(err) }
    defer resp.Body.Close()
    b, _ := io.ReadAll(resp.Body)
    fmt.Println(string(b))
}

Privacy and Data Handling

All conversion happens locally in your browser. Your cURL commands — including API keys, auth tokens, and request payloads — never leave your device. Safe to use with development and staging credentials.

Frequently Asked Questions

Does the generated Go code handle errors properly?

The converter generates Go code with standard error checks after each fallible operation — http.NewRequest, client.Do, and io.ReadAll. For production use, replace the panic(err) stubs with proper error returns or logging matching your application's error handling pattern. The generated code is a working starting point, not a production-hardened implementation.

How do I set a timeout on the generated Go HTTP client?

The converter uses http.DefaultClient which has no timeout. For production code, create a custom client: client := &http.Client{Timeout: 10 * time.Second} and replace http.DefaultClient.Do(req) with client.Do(req). Always set timeouts on HTTP clients in Go — the default client with no timeout can hang indefinitely on slow servers.

How do I handle the response body in Go?

Always call defer resp.Body.Close() immediately after checking the error from client.Do(). Failing to close the body causes resource leaks. To decode JSON responses, use json.NewDecoder(resp.Body).Decode(&target) instead of reading all bytes first. Check resp.StatusCode before decoding — non-2xx responses may return error JSON that differs from success responses.