2.9 KiB
| name | description |
|---|---|
| curl-server-action | Technique for driving this project's Next.js Server Actions via curl, without a browser — for manual/live verification of forms and mutations. Use whenever you need to test a Server Action (login, form submit, a drag-triggered action) from the command line. |
Driving Next.js Server Actions with curl
Next.js Server Actions ("use server" functions used as <form action={fn}> or called directly) don't accept a plain -d key=value POST — the framework encodes the action reference into the request, and the encoding differs depending on whether the action is bound or unbound.
Step 1: find the action encoding in the rendered HTML
curl -s http://localhost:<port>/<page> -o page.html
Unbound action (<form action={someFn}>, no .bind()): look for a single hidden field:
grep -o '\$ACTION_ID_[a-z0-9]*' page.html
This hash changes on every dev-server restart/rebuild — always re-read it fresh from the current page load, never reuse a previously-seen value.
Bound action (someFn.bind(null, arg1, arg2)): look for a triple of fields instead:
grep -o '\$ACTION_REF_[a-z0-9]*\|\$ACTION_[a-z0-9]*:[01]' page.html
$ACTION_REF_<n>— empty-valued field referencing the action$ACTION_<n>:0— bound marker, value"$@1"$ACTION_<n>:1— JSON array of the bound arguments, e.g.["abholort"]
Step 2: submit with the matching fields
Unbound:
HASH=$(curl -s http://localhost:<port>/login | grep -o '\$ACTION_ID_[a-z0-9]*' | head -1)
curl -s -c cookies.txt -X POST http://localhost:<port>/login \
-F "${HASH}=" \
-F 'email=admin@aushilfsfahrer.local' \
-F 'password=Admin123!' \
-L -o response.html -w "HTTP_STATUS:%{http_code} FINAL_URL:%{url_effective}\n"
Bound (three extra -F fields instead of one):
curl -s -b cookies.txt -X POST http://localhost:<port>/some-page \
-F '$ACTION_REF_3=' \
-F '$ACTION_3:0={"id":"...","bound":"$@1"}' \
-F '$ACTION_3:1=["abholort"]' \
-F 'other_form_field=value' \
-L -o response.html -w "HTTP_STATUS:%{http_code} FINAL_URL:%{url_effective}\n"
Reuse the cookie jar (-b cookies.txt -c cookies.txt) across requests to stay authenticated.
Gotchas
- If a page renders multiple Server Action forms at once (e.g. a page with both a logout button and a content form), a naive
grep | head -1may grab the wrong action's ID. Narrow the grep with surrounding context (e.g.grep -B2 -A2 'Submit-Button-Label') or inspect the embedded JSON payload in the HTML, which includes"name":"<functionName>"next to the matching action id — use that to confirm you've got the right one. - A plain
.env.localmisconfiguration (pointing at the wrong Supabase stack — see thesupabase-local-devskill) will make login appear to fail with a generic error even though the curl technique itself is correct. Verify the target stack before assuming the encoding is wrong.