marblejs / marble

Marble.js - functional reactive Node.js framework for building server-side applications, based on TypeScript and RxJS.
https://marblejs.com
MIT License
2.15k stars 69 forks source link

req$ -> body equal to undefined #299

Closed dwfe closed 3 years ago

dwfe commented 3 years ago

Describe the bug I send post request from client:

  const sendDataToServer = () =>
    fetch('/long-answer', {
      method: 'post',
      headers: {
        'Content-Type': 'text/plain;charset=utf-8',
      },
      body: `is body exist?`
    })
      .then(response => response.text())
      .then(body => console.log(`body:`, body))
      .catch(e => console.error(`fetch`, e))

and on the server side I get body as undefined:

export const api_long_answer$ = r.pipe(
  r.matchPath('/long-answer'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    tap(({method, url, body}) => console.log(`post req`, {method, url, body})),
    delay(3_000),
    map(({body}) => ({
      headers: {
        'Content-Type': 'text/plain;charset=utf-8'
      },
      body
    })),
  ))
);

To Reproduce

  1. https://github.com/MyTempGitForCommunity/marble-body-undefined
  2. run script "server:run"
  3. open in browser http://127.0.0.1:1337/
  4. push the button 'Send Data To Server'
  5. view the log in the server console. In my case, I see the following: post req { method: 'POST', url: '/long-answer', body: undefined }

Expected behavior In theory if I understood everything correctly the body should not be equal to undefined.

Desktop (please complete the following information):

JozefFlakus commented 3 years ago

Thanks for posting an issue. What will happen if you omit charset part?

dwfe commented 3 years ago

Same, body equal to undefined:

without-content

dwfe commented 3 years ago

Sorry, if I omit only charset part:

      headers: {
        'Content-Type': 'text/plain',
      },

it works correctly body is present on server side

JozefFlakus commented 3 years ago

Thanks, looks like parameters are not properly detected which results in not-matched parser. Package scope: @marblejs/middleware-body

JozefFlakus commented 3 years ago

Fixed with version v3.4.4.