--- name: curl-server-action description: 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 `
` 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 ```bash curl -s http://localhost:/ -o page.html ``` **Unbound action** (``, no `.bind()`): look for a single hidden field: ```bash 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: ```bash grep -o '\$ACTION_REF_[a-z0-9]*\|\$ACTION_[a-z0-9]*:[01]' page.html ``` - `$ACTION_REF_` — empty-valued field referencing the action - `$ACTION_:0` — bound marker, value `"$@1"` - `$ACTION_:1` — JSON array of the bound arguments, e.g. `["abholort"]` ## Step 2: submit with the matching fields Unbound: ```bash HASH=$(curl -s http://localhost:/login | grep -o '\$ACTION_ID_[a-z0-9]*' | head -1) curl -s -c cookies.txt -X POST http://localhost:/login \ -F "${HASH}=" \ -F 'identifier=admin@aushilfsfahrer.local' # Feld heisst identifier (E-Mail oder Benutzername) \ -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): ```bash curl -s -b cookies.txt -X POST http://localhost:/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 -1` may 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":""` next to the matching action id — use that to confirm you've got the right one. - A plain `.env.local` misconfiguration (pointing at the wrong Supabase stack — see the `supabase-local-dev` skill) 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.