Last updated
cURL to Code Converter Examples
The cURL to Code Converter transforms curl commands into equivalent code in your target language. Below are examples showing the same request converted to multiple languages.
Input curl Command
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer token123" \
-d '{"name": "Alice", "email": "alice@example.com"}'
JavaScript — fetch (async/await)
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
},
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com'
})
});
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
console.log(data);
JavaScript — axios
import axios from 'axios';
const response = await axios.post('https://api.example.com/users', {
name: 'Alice',
email: 'alice@example.com'
}, {
headers: {
'Authorization': 'Bearer token123'
}
});
console.log(response.data);
Python — requests
import requests
response = requests.post(
'https://api.example.com/users',
headers={
'Authorization': 'Bearer token123'
},
json={
'name': 'Alice',
'email': 'alice@example.com'
}
)
response.raise_for_status()
data = response.json()
print(data)
PHP — cURL
$ch = curl_init('https://api.example.com/users');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer token123'
],
CURLOPT_POSTFIELDS => json_encode([
'name' => 'Alice',
'email' => 'alice@example.com'
])
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
Go — net/http
package main
import (
"bytes"
"encoding/json"
"net/http"
)
body, _ := json.Marshal(map[string]string{
"name": "Alice",
"email": "alice@example.com",
})
req, _ := http.NewRequest("POST", "https://api.example.com/users", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer token123")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
Ruby — Net::HTTP
require 'net/http'
require 'json'
require 'uri'
uri = URI('https://api.example.com/users')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request['Content-Type'] = 'application/json'
request['Authorization'] = 'Bearer token123'
request.body = { name: 'Alice', email: 'alice@example.com' }.to_json
response = http.request(request)
data = JSON.parse(response.body)
Java — HttpClient (Java 11+)
import java.net.http.*;
import java.net.URI;
var client = HttpClient.newHttpClient();
var body = """
{"name": "Alice", "email": "alice@example.com"}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users"))
.POST(HttpRequest.BodyPublishers.ofString(body))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer token123")
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
File Upload curl → Python
Input:
curl -X POST https://api.example.com/upload \
-F "file=@report.pdf" \
-H "Authorization: Bearer token123"
Python output:
import requests
with open('report.pdf', 'rb') as f:
response = requests.post(
'https://api.example.com/upload',
headers={'Authorization': 'Bearer token123'},
files={'file': f}
)
response.raise_for_status()
print(response.json())
Common Use Cases
- Converting API documentation curl examples into application code
- Translating curl commands from runbooks into production code
- Learning HTTP client syntax for a new programming language
- Quickly prototyping API integrations from curl examples
- Converting curl commands shared by teammates into your language
Paste any curl command and select your target language to get clean, idiomatic code with proper error handling ready to use in your application.