fix(network): request.postData() returns null for empty string body override#41882
Conversation
dcrousso
left a comment
There was a problem hiding this comment.
this looks like a good fix (minus the \n issue) but it needs a test
| ].join(' | ||
| ')); |
There was a problem hiding this comment.
oops? i think you may have accidentally replaced all \n with an actual newline elsewhere in this file
…ore escaped newlines
|
Thanks for catching those — addressed in the latest push:
|
| page.on('request', request => { | ||
| if (request.url().includes('empty-post')) | ||
| captured.push(request.postData()); | ||
| }); |
There was a problem hiding this comment.
i dont think this will work as im pretty sure it runs before the route applies
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Fixed the test — was using |
Test results for "tests 1"2 flaky50006 passed, 1189 skipped Merge workflow run. |
Test results for "MCP"48 failed 7712 passed, 1249 skipped Merge workflow run. |
Summary
request.postData()incorrectly returnsnullwhen the POST body is set to an empty string viaroute.continue({ postData: '' })orroute.fallback({ postData: '' }).Root cause
In
packages/playwright-core/src/client/network.ts, thepostData()method uses|| nullat the end:When the override is set to an empty string,
Buffer.from('', 'utf-8')is stored in_fallbackOverrides.postDataBuffer. Since an emptyBufferis a truthy object, the left-hand side resolves correctly to the empty buffer, and.toString('utf-8')yields''. However,'' || nullevaluates tonull, discarding the empty-string body.This is inconsistent with
postDataBuffer(), which returns the correct emptyBufferfor the same input.Fix
Switch the trailing
|| nullto?? null(nullish coalescing), which only falls back tonullwhen the value isnullorundefined, not when it is an empty string:The inner
||is similarly changed to??so that a zero-length override buffer isn't accidentally skipped in favour of the original request's body.Reproduction