Skip to content

Fix RPC calls with a form body in live mode #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: lamdera-next
Choose a base branch
from

Conversation

hansallis
Copy link

@hansallis hansallis commented May 28, 2025

Quick Summary:
RPC requests with an application/x-www-form-urlencoded body are transformed into JSON. In live mode however, strings were escaped as JSON and then surrounded (again) by quotes. This led to an invalid JSON string being sent to the browser window running the backend, causing a SyntaxError there and a timeout on the request.

SSCCE

  1. lamdera init
  2. Add the example RPC endpoint from the docs
  3. lamdera live
  4. POST a form:
curl -X POST http://localhost:8000/_r/hello-world \
   -H "Content-Type: application/x-www-form-urlencoded" \
   -d "param1=value1&param2=value2&param2=value3&test=4"
  1. Observe the browser window running the backend:
SyntaxError: Expected ',' or '}' after property value in JSON at position 12 (line 1 column 13)
    at JSON.parse (<anonymous>)
    at M (...:68885:5691)
    at WebSocket.onmessage (...:68885:3228)
{
    "t": "q",
    "s": "9a2e567f38d61cf28c78aaa726c5e3b67e4bc828",
    "e": "hello-world",
    "r": "8a6204ff-dc23-43be-9ea3-b6a5b0a7f915",
    "h": {
        "content-length": "48",
        "content-type": "application/x-www-form-urlencoded",
        "accept": "*/*",
        "user-agent": "curl/8.7.1",
        "host": "localhost:8000"
    },
    "j": "{\"param1\":\"\"value1\"\",\"param2\":[\"\"value2\"\",\"\"value3\"\"],\"test\":\"\"4\"\"}"
}

hansallis added 2 commits May 28, 2025 13:52
- Introduced `generateRpcRequestPayload` function to handle the creation of RPC request payloads based on content type and request data.
- Updated `serveRpc` to utilize the new payload generation function.
- Added a new test suite for `Test.Lamdera.Live` to validate form-urlencoded parsing logic and ensure correct JSON structure in the generated payload.
- Included the new test  in `elm.cabal` and `Test.hs`
@hansallis
Copy link
Author

I've added a test to demonstrate, not sure if you want to keep that, but it was a fun Claude/Haskell co-op.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant