curl to Python Converter

Paste a curl command from your browser developer tools or your terminal and get an equivalent Python script that uses the requests library. The generated code is complete and runnable, so it saves you from rebuilding the headers, the query string and the request body by hand.

curl command

Python code


            
            

Ready — paste some input or load the sample.

100% Client-Side · Privacy First. This tool runs entirely inside your browser. Nothing you type is uploaded, logged or sent to a server — you can even use it offline.

Overview

Converting a curl command into a Python script is one of those tasks that looks trivial until the script fails to run. The command line has to be tokenised correctly, because a header value may contain spaces, a body may contain an equals sign, and a line ending in a backslash continues onto the next line. Then the options have to map onto the requests library, and the mapping is not one to one: a JSON body becomes a Python dictionary rather than a JSON string, form fields become a dictionary, a file upload becomes a files dictionary, and basic authentication becomes an auth tuple. This converter handles all of that. The generated code uses requests, prints the status and parses a JSON response, and every value is quoted so that a stray quote in a token cannot break the script. The result is code you can paste into a script and run as it is.

How to use the curl to python converter

  1. 1Copy the curl command from the browser network tab or straight out of your shell history.
  2. 2Paste it into the input panel.
  3. 3Read the generated Python and adjust anything that was specific to your environment, such as the timeout or the base URL.
  4. 4Install the one dependency with pip install requests.
  5. 5Run the script with python3 script.py and compare its output with the original curl command.
  6. 6Check the notes for the HTTP method and for any flag that was dropped, so nothing important changes silently.

Cheat sheet

The syntax you will use most, and what each part does.

PatternWhat it means
-X POST Becomes the method argument of requests.request, defaulting to GET or POST depending on whether there is a body.
-H "Name: value" Becomes an entry in the headers dictionary.
-d '{"a":1}' With a JSON content type it becomes a Python dict passed as json=payload.
-u user:pass Becomes an auth tuple of (user, pass).
-b "name=value" Becomes a cookies dictionary.
-k Becomes verify=False, with a comment that certificate verification was disabled.

Common mistakes

The generated code is not valid Python.

The JSON body is converted to a Python literal with True, False and None instead of JSON true, false and null, and every string is quoted with Python escaping.

The URL contains a newline.

A backslash at the end of a line is a line continuation in the shell, so it is removed before the URL is parsed.

The user agent is missing.

A -A option is added to the headers dictionary rather than being applied to an undefined variable.

A form field ends up as a string.

A -d without a JSON content type becomes a form encoded body passed as data=payload, which is what requests expects.

Worked examples

A JSON POST

curl -X POST https://api.example.com/items -H 'Content-Type: application/json' -d '{"name":"x","ok":true}'

A script that builds a Python dictionary and sends it with json=payload.

A request with basic auth

curl -u user:pass https://api.example.com/me

A script with an auth tuple passed to requests.request.

Frequently asked questions

Which Python library is used?

The requests library, because it covers every part of a curl command in one readable object: the method, the query parameters, the headers, the JSON body, form fields, file uploads and basic authentication.

Does the generated code run as it is?

Yes, as long as requests is installed. The script defines the URL, the parameters, the headers and the payload, calls requests.request with a thirty second timeout and prints the status code and the response body.

How is a file upload handled?

A form field whose value starts with an at sign is treated as a file path and becomes an entry in a files dictionary, opened in binary mode and passed through the files argument of the request.

Is the curl command sent to a server?

No. The command is parsed in your browser, so a command that contains a session cookie or an internal hostname never leaves your machine.

Why is a JSON body turned into a Python dictionary?

Because requests can serialise a dictionary for you and set the correct Content-Type header. A true, false and null in the JSON become True, False and None, which is what Python expects.

What happens to the query string?

It is split into a params dictionary, so each key and value is passed as a separate argument. That is safer than pasting a raw query string, because requests escapes the values for you.

How are cookies handled?

A cookie flag becomes a cookies dictionary that is handed to the request as an argument. Nothing is stored in a jar, so every call is independent and repeatable.

Can the script be turned into a reusable function?

Yes. Wrap the body of the script in a def that takes the changing parts as arguments, and pass the headers and the payload in from the caller instead of rebuilding them for every request.

Why is the timeout set to thirty seconds?

Because a request without a timeout can hang forever. Thirty seconds is long enough for an ordinary API call and short enough that a stuck connection does not block the whole script.

How do I turn the script into a reusable client?

Put the URL, the headers and the payload into a function that takes them as arguments, and let the caller decide what to do with the response. That is the difference between a one-off script and a small client.

Can I use the requests session for connection pooling?

Yes, and you should when you make many calls. Create one session, set the headers on it and reuse it, so the underlying TCP connection is kept alive between the requests.