curl to Python requests: <Response [412]> or 500 fix
curl works but Python requests returns <Response [412]> or 500? Fix the cookie jar with a Session, --data-urlencode encoding, headers and redirects.
TroubleshootingUpdated 7 min read
On this page
You converted a curl command to Python requests, and instead of the response you saw in the terminal you get <Response [412]> or a 500. People report two common versions of this: a login-then-status flow that only works once the session cookie is carried over, and a --data-urlencode 'params[...]=...' example that returns a 500 from converter output.
A converter translates one curl command at a time. It cannot know an earlier command created the cookie, and it cannot tell you why a server rejected the request. You have to compare the two requests yourself.
Why the converted request fails
Causes, most common first:
- A cookie is missing. curl read it from
-bor a-c/-bjar. The converted code is a fresh request with no cookie. - The body is encoded differently.
--data-urlencodeand nested keys likeparams[name]do not map one-to-one onto a Python dict. - Headers differ. curl and
requestssend different defaults forUser-AgentandContent-Type, and some servers or firewalls react to that. - Redirects behave differently.
requestsfollows them by default, curl does not unless you pass-L. - The original command is stale. A cookie or token copied from a browser or an old run has expired.
Which one applies depends on the server, and a status like 412 does not say. A 412 means a precondition the server checks was not met; what that precondition is differs per API.
| Symptom | Likely cause | Fix |
|---|---|---|
| Second call of a login flow fails | No cookie carried over | Use one Session |
500 with params[...] fields | Nested keys encoded differently | Flat bracket keys, compare bodies |
| Fails only from Python | Different headers | Copy every -H and -A |
| Different status or body than curl | Redirect followed | allow_redirects=False |
| curl fails too | Stale token or cookie | Log in again |
Step-by-step fixes
1. Carry the session cookie with a Session
A two-step curl flow usually looks like this:
curl -c jar.txt -d 'user=alice' -d 'pass=secret' https://example.com/login
curl -b jar.txt https://example.com/status-c jar.txt saves the cookies the server sets. -b jar.txt sends them back. Converted line by line, you get two unrelated calls, and the second has no cookie. Use one Session, which keeps a cookie jar:
import requests
s = requests.Session()
r = s.post("https://example.com/login", data={"user": "alice", "pass": "secret"})
print(r.status_code, s.cookies.get_dict()) # is the session cookie here?
r = s.get("https://example.com/status")
print(r.status_code)If s.cookies.get_dict() is empty after login, the server did not set a cookie, usually because the login failed. Print r.status_code, r.history (each redirect hop) and r.headers.get("Set-Cookie").
If curl passes a literal cookie such as -b 'sessionid=abc123', send it per request:
r = requests.get("https://example.com/status", cookies={"sessionid": "abc123"})2. Compare what curl and requests actually send
The converted code is a guess at what curl did. Look at both requests. On the curl side, add -v; lines starting with > are the request headers. Verbose output goes to stderr, and it changes nothing:
curl -v -o /dev/null -b 'sessionid=abc123' https://example.com/statusOn the Python side, inspect the prepared request after the call:
r = s.get("https://example.com/status")
print(r.request.url)
print(r.request.headers)
print(r.request.body)Check line by line: the URL (including %5B / %5D encoding in the query), whether a Cookie: header exists, Content-Type, Accept, User-Agent, and the body character for character.
To compare raw bytes, run nc -l 8080 in one Terminal window, then point your curl command and your Python script at http://localhost:8080/... in another. The listener prints each raw request. Stop each client with Control-C, since they wait for a reply. This works for literal cookies and headers; cookies in a Session jar are tied to the original domain and will not be sent to localhost.
3. Check how --data-urlencode and nested params are encoded
With --data-urlencode 'name=content', curl encodes the content part and sends the name part as you typed it, so params[name] goes out unchanged. requests encodes dictionary keys, so the same key goes out as params%5Bname%5D. Most servers decode both the same; a strict one may not.
requests also does not expand a nested dictionary into params[name]=x keys. Use flat keys with the brackets in the name:
data = {"params[name]": "some value", "params[page]": "2"}
r = s.post("https://example.com/search", data=data)
print(r.request.body)To see curl's body byte for byte, add --trace-ascii - to the curl command; it prints sent and received data to the terminal.
Also check data= versus params=. With -G, curl appends the --data-urlencode pairs to the URL as a query string, so the Python equivalent is params=. Without -G they are the POST body, so use data=.
4. Match the headers curl sends
| Item | curl | requests |
|---|---|---|
-d with no Content-Type | Sends application/x-www-form-urlencoded | Sets it only when data= is a dict or list of pairs; a plain string body gets none |
User-Agent | A curl/… string | A python-requests/… string |
| Cookies | Only with -b | Automatic inside a Session |
| Redirects | Not followed unless -L | Followed by default |
If you send the body as a string, set the header yourself:
headers = {"Content-Type": "application/x-www-form-urlencoded"}
r = s.post(url, data="a=1&b=2", headers=headers)Copy every -H from your curl command into headers=. If the server filters on User-Agent, use the value curl sent (the -A value, or the User-Agent: line in curl -v).
5. Match redirects and the HTTP method
requests follows redirects by default, and a POST that gets a 301 or 302 is re-sent as a GET. curl without -L stops at the first response. Look for a Location: header in curl -v, then turn following off to match:
r = s.post(url, data=data, allow_redirects=False)
print(r.status_code, r.headers.get("Location"))Also check the method: -d makes a POST in curl even without -X POST, and -G turns it into a GET.
6. Confirm the original curl command still works
Re-run the original curl command on its own. If curl also returns 500 or 412, the conversion is not the problem; the request lacks something the server needs, such as a fresh token or a valid cookie. A cookie that worked yesterday and returns 412 today needs a new login.
Checklist before you try again
- One
Sessionfor every call that shared a cookie jar in curl. - Every
-H,-Aand-bvalue from curl is present inr.request.headers. r.request.bodymatches curl's--trace-ascii -output.data=orparams=matches whether-Gwas used.- Redirect behavior matches (
-Lor not). - The curl command itself still succeeds today.
What Bytepliers covers here, and what it doesn't
Bytepliers has a cURL → Code tool. Paste a command, open the Python requests tab and copy the code. It parses -b, --data-urlencode and -G, plus -X -H -d --data-raw --data-binary --json -F -u -A -e -I -L -k --compressed --url. Its request summary lists method, URL, headers, body size and form fields, and names options it could not translate, which helps you spot a dropped header or body.
Run (⌘↩) sends the pasted command from your Mac and shows status, timing, body and, under Show response headers, the headers. It sends only what the command contains and keeps no cookies between runs, so for a login flow you would copy the Set-Cookie value into -b on the second command. The first request to each host asks for confirmation.
It does not parse -c, does not add a requests.Session() for you, and cannot explain why a server returned 412 or 500. For conversion only, see Convert a cURL command to Swift, fetch or Python. Bytepliers outputs three targets: Swift URLSession, JavaScript fetch and Python requests.
If nothing works
If curl works and a Python request that matches it byte for byte still fails, the server may treat the two clients differently. Contact the API vendor or server owner and include the URL, method, time and status, both raw requests (curl -v and the Python prepared request) with secrets redacted, the response body and any request ID header.
Subject: 412 from Python requests, 200 from curl on the same endpoint
Hi, a POST to https://example.com/login followed by GET /status works
with curl but returns <Response [412]> from Python requests at
2026-11-13 10:42 UTC (request ID: ...).
Attached: curl -v output and r.request.headers/body from Python, tokens
redacted. I use one requests.Session and have matched headers and
cookies. Can you tell me which precondition the second request fails?Common questions
Why does curl work without a Session?
curl does not keep cookies unless you give it -b or -c. Your earlier command wrote or supplied them explicitly, and requests needs a Session to do the same implicitly.
Is 412 always about cookies?
No. It means a precondition the server checks was not met. Cookies are the most common cause people report here, but a missing header or an expired token can produce it too.
Why does a nested dict not work as params[name]?
requests does not expand nested dictionaries into bracket keys. Use flat keys like "params[name]" and compare r.request.body with curl's output.
Can I make requests look exactly like curl?
Set the same User-Agent, copy every -H, and use allow_redirects=False unless curl used -L. Then diff the raw requests with nc -l 8080.