Skip to content

Conversation

@hansallis
Copy link
Contributor

@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
Contributor 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.

@supermario supermario force-pushed the fix-live-form-encoded-rpc branch from 7b70b71 to 5212837 Compare August 9, 2025 04:20
@supermario
Copy link
Member

Thanks @hansallis – have dropped the test as per our discussion, we can refine later.

@supermario supermario merged commit c0ff74e into lamdera:lamdera-next Aug 9, 2025
supermario added a commit that referenced this pull request Aug 11, 2025
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.

2 participants