𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗖𝗨𝗥𝗟 𝘁𝗼 𝗣𝘆𝘁𝗵𝗼𝗻 𝗥𝗲𝗾𝘂𝗲𝘀𝘁𝘀 𝗦𝘁𝗲𝗽 𝗕𝘆 𝗦𝘁𝗲𝗽
You copy a request from your browser DevTools as a cURL command.
You get the headers, cookies, and auth tokens.
But you end up with 30 lines of messy shell code. You wanted clean Python.
Here is how you map a cURL command to the Python requests library manually.
Determine the Method If you see --data or --data-raw, curl uses POST by default. Do not assume it is a GET request just because the -X flag is missing.
Handle Headers Every -H flag becomes an entry in a dictionary. When you split the header string, only split on the first colon. Values like URLs or timestamps contain colons. If you split on every colon, you will break the data.
Manage Cookies The -b flag provides a semicolon-separated string. Do not put this in your headers dictionary. Pass it to the cookies argument as a dictionary instead. This lets the library handle the encoding.
Use JSON vs Data This is a common mistake. If the content type is application/json, use the json= argument. This serializes your dictionary and sets the correct header for you. If you use data=, requests will use form-encoding. This will cause the API to reject your request.
Clean Up Parameters Move the query string from the URL into a params dictionary. This makes your code readable. Watch out for double-encoding. If the URL is already encoded, do not encode it again in your dictionary.
The final code structure looks like this:
import requests
url = "https://api.example.com/v2/search" params = {"lang": "en", "page": "2"} headers = { "accept": "application/json", "authorization": "Bearer token_here", "referer": "https://app.example.com/dashboard", } cookies = {"session": "abc123"} payload = {"q": "nginx"}
resp = requests.post( url, params=params, headers=headers, cookies=cookies, json=payload, )
resp.raise_for_status() print(resp.json())
Common traps to avoid:
- Mixing up files= with data= during multipart uploads.
- Splitting headers on every colon.
- Double-encoding query parameters.
Source: https://dev.to/wanfeng/from-browser-curl-to-clean-python-requests-code-step-by-step-25oh