Translating cURL to Go, Fetch, and Python Requests
Learn how to instantly translate bash cURL commands into production-ready Go, JavaScript, and Python code using browser-based developer tools.
Why Translate cURL?
cURL is the universal language of API documentation. Almost every API provider gives examples as cURL commands because they run universally in bash terminals.
However, the workflow doesn't stop in the terminal. Once you've verified an endpoint works, you need to embed that logic into your application. Translating complex cURL flags into HTTP library code manually is tedious and prone to syntax errors.
Anatomy of a cURL Command
Understanding the flags is the first step to translating them:
| Flag | Meaning | Example |
|---|---|---|
-X / --request |
The HTTP method | -X POST |
-H / --header |
Custom headers (auth, content-type) | -H "Content-Type: application/json" |
-d / --data |
Request body payload | -d '{"key":"value"}' |
-b / --cookie |
Send cookies | -b "session=123" |
-u / --user |
Basic Authentication | -u "username:password" |
-k / --insecure |
Skip TLS/SSL verification | -k |
Code Generation Patterns
cURL to Go (net/http)
Go's standard library is powerful but verbose. A simple POST request requires creating a reader, building the request, executing it, and explicitly handling errors.
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
jsonData := []byte(`{"title":"foo","body":"bar","userId":1}`)
req, _ := http.NewRequest("POST", "https://jsonplaceholder.typicode.com/posts", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_TOKEN")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
cURL to JavaScript (Fetch vs Axios)
In the browser or modern Node.js environments, fetch() is the standard. However, many developers still prefer Axios for its automatic JSON parsing and robust error handling.
Using Fetch:
const response = await fetch("https://api.example.com/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
},
body: JSON.stringify({ key: "value" })
});
if (!response.ok) throw new Error("Network response was not ok");
const data = await response.json();
cURL to Python (requests)
Python's requests library abstracts away the complexity of HTTP calls into an incredibly readable API.
import requests
url = "https://api.example.com/data"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
}
payload = {"key": "value"}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status() # Raises an exception for 4XX/5XX errors
print(response.json())
Handling Authentication Across Languages
Authentication patterns vary slightly:
- Bearer Tokens: Universally passed via the
Authorization: Bearer <token>header. - Basic Auth: While you can manually encode credentials as Base64, libraries usually offer helpers (e.g., the
auth=(user, pass)tuple in Pythonrequests, orreq.SetBasicAuth(user, pass)in Go).
File Uploads (Multipart Form Data)
Translating a cURL -F "file=@/path/to/file" requires careful handling:
- Go: Requires the
mime/multipartpackage to construct the form boundary. - JavaScript: Use the
FormDataobject. Do not manually set theContent-Typeheader; let the browser generate it with the boundary! - Python: Pass a dictionary to the
filesparameter inrequests.post().
Need to translate a command right now? Use our in-browser code generators: cURL to Go cURL to Python cURL to Fetch
Explore more in our specific language guides: cURL to Python Deep Dive and cURL to Fetch/Axios.
Live Tool: cURL to Go HTTP Request
Client-Side EngineTranslate bash cURL commands into Go `net/http` client code.