Back to Home
Updated: June 20265 min read

How to Convert cURL to Fetch — Developer Guide

Converting cURL commands to JavaScript fetch calls is a common task when moving from API documentation to implementation. Use our cURL to Fetch Converter for automatic conversion.

Why Convert cURL to Fetch?

API documentation almost always provides cURL examples, but modern web applications use the Fetch API for HTTP requests. Manually converting between them is tedious and error-prone — especially for complex requests with headers, authentication, and request bodies.

Basic Conversion Examples

// cURL: GET request
curl https://api.example.com/users

// Fetch equivalent
const res = await fetch('https://api.example.com/users')
const data = await res.json()
// cURL: POST with JSON body
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

// Fetch equivalent
const res = await fetch('https://api.example.com/users', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'John', email: 'john@example.com' })
})
const data = await res.json()

Advanced: cURL with Authentication

// cURL: Bearer token auth
curl -X GET https://api.example.com/profile \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."

// Fetch equivalent
const res = await fetch('https://api.example.com/profile', {
  headers: {
    'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIs...'
  }
})
const data = await res.json()
// cURL: Basic auth
curl -u admin:secret https://api.example.com/admin

// Fetch equivalent (manual Basic auth header)
const token = btoa('admin:secret')
const res = await fetch('https://api.example.com/admin', {
  headers: { 'Authorization': `Basic ${token}` }
})

Common Pitfalls When Converting

cURL follows redirects by default. Fetch follows redirects by default too, but if you use redirect: 'manual', you need to handle 302 responses yourself.

cURL shows response headers automatically. With fetch, use res.headers to access response headers after the request completes.

Cookies need extra handling. cURL -b sends cookies automatically. In fetch, include credentials: 'include' for cross-origin requests with cookies.

cURL -k ignores SSL errors. Browsers do not allow disabling SSL verification for security reasons. Use valid certificates in production.

Related Tools