cURL to Code Converter - fetch, Python, Go, PHP
Convert cURL commands to code
API docs give their examples as curl commands, and every browser can copy a network request as one. Rewriting that by hand means re-typing headers, re-escaping quotes and looking up how each library sends JSON or a multipart form. Paste the command here and get code for JavaScript fetch, Axios, Python requests, Go net/http, PHP cURL or Node.js https.
How to use it
Paste a command and pick a language. Switching language re-renders the same request, so you can compare them side by side. If the command cannot be read, such as no URL or a flag with no value after it, the error says which.
Example
Input:
curl 'https://api.example.com/v1/orders' \
-H 'Authorization: Bearer sk_test_123' \
-H 'Content-Type: application/json' \
--data-raw '{"product":"tea","qty":2}' Output (Python requests):
import requests
headers = {
"Authorization": "Bearer sk_test_123",
}
json_data = {
"product": "tea",
"qty": 2,
}
response = requests.post("https://api.example.com/v1/orders", headers=headers, json=json_data)
print(response.status_code, response.text)
The method is POST because the command sends data, and the JSON body becomes a Python dictionary passed with
json=, which also sets the Content-Type header for you.
How curl options map to code
- Method: taken from
-X. Without it, requests with data become POST,-Ibecomes HEAD and everything else is GET. - Headers: each
-Hbecomes a header.-bbecomes a Cookie header,-Aa User-Agent. - Body: several
-dflags are joined with&, as curl does.--jsonalso adds JSON Content-Type and Accept headers. - Query string: with
-G, the data is appended to the URL instead of sent as a body. - Forms and files:
-F name=valueand-F file=@pathbecome multipart form data with a file upload. - Basic auth:
-u user:passuses each library's auth option or an Authorization header. - TLS and redirects:
-kturns off certificate checks and-Lfollows redirects where the library needs to be told. Browserfetchcannot skip certificate checks, so the fetch output only adds a comment saying so.
Tips for browser requests
Requests copied from DevTools carry headers the browser adds itself: sec-ch-ua, sec-fetch-*,
accept-language, priority. APIs rarely need them, so delete them to keep the code readable. Keep
authorization, content-type, cookies and custom x- headers. If the request still fails
outside the browser, the server is probably checking a cookie or CSRF token that has since expired. To see what a server sends
back, try the
HTTP headers inspector.
Related Tools
Epoch / Unix Timestamp Converter
Convert between Unix epoch timestamps and human-readable dates.
Regex Tester
Test regular expressions against text with match highlighting.
UUID Generator
Generate random v4 UUIDs for use in databases and APIs.
Colour Converter
Convert colours between HEX, RGB, and HSL formats.