DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

cURL to Code: Convert cURL to JavaScript, Python, Go Instantly

API docs give you cURL. Your app needs fetch/requests/axios. Here's the complete conversion reference.

The cURL Command

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxx" \
  -d '{"name": "Alice", "email": "alice@example.com"}'
Enter fullscreen mode Exit fullscreen mode

→ JavaScript (fetch)

const response = await fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer sk-xxxxx',
  },
  body: JSON.stringify({
    name: 'Alice',
    email: 'alice@example.com',
  }),
});
Enter fullscreen mode Exit fullscreen mode

→ Python (requests)

import requests

response = requests.post(
    'https://api.example.com/users',
    headers={'Authorization': 'Bearer sk-xxxxx'},
    json={'name': 'Alice', 'email': 'alice@example.com'},
)
Enter fullscreen mode Exit fullscreen mode

→ Go (net/http)

payload := strings.NewReader(`{"name":"Alice","email":"alice@example.com"}`)
req, _ := http.NewRequest("POST", "https://api.example.com/users", payload)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer sk-xxxxx")
resp, _ := http.DefaultClient.Do(req)
Enter fullscreen mode Exit fullscreen mode

cURL Flags Cheat Sheet

Flag Meaning
-X POST HTTP method
-H "..." Add header
-d '...' Request body
-u user:pass Basic auth
-F "file=@img.jpg" File upload
-b "session=abc" Send cookies
-k Skip SSL verify
-L Follow redirects
-v Verbose/debug

Try It Online

Convert any cURL command to code with DevToolBox's cURL Converter — supports JavaScript, Python, Go, PHP, Ruby, and more.


What's the most complex cURL you've had to convert? Share below!

Top comments (0)