Skip to content

Commit 797cfc4

Browse files
authored
Merge pull request #61 from BAIXIONGSODA/fix/post-data
fix: preserve raw request data to fix [object Object] display issue
2 parents 287c02c + 89e7ef7 commit 797cfc4

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

apps/koa-esm/src/index.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,45 @@ router.get('/fetch', async (ctx) => {
7777
ctx.body = data
7878
})
7979

80+
// Test case 1: text/plain + JSON body
81+
router.get('/post-text-plain', async (ctx) => {
82+
const res = await axios.post(
83+
'https://jsonplaceholder.typicode.com/posts',
84+
JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }),
85+
{ headers: { 'Content-Type': 'text/plain' } }
86+
)
87+
ctx.body = res.data
88+
})
89+
90+
// Test case 2: application/x-www-form-urlencoded
91+
router.get('/post-form', async (ctx) => {
92+
const res = await axios.post(
93+
'https://jsonplaceholder.typicode.com/posts',
94+
'title=foo&body=bar&userId=1',
95+
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
96+
)
97+
ctx.body = res.data
98+
})
99+
100+
// Test case 3: pure text body
101+
router.get('/post-pure-text', async (ctx) => {
102+
const res = await axios.post(
103+
'https://jsonplaceholder.typicode.com/posts',
104+
'This is a plain text message',
105+
{ headers: { 'Content-Type': 'text/plain' } }
106+
)
107+
ctx.body = res.data
108+
})
109+
110+
// Test case 4: Buffer body
111+
router.get('/post-buffer', async (ctx) => {
112+
const buffer = Buffer.from(JSON.stringify({ title: 'buffer', body: 'test', userId: 1 }))
113+
const res = await axios.post('https://jsonplaceholder.typicode.com/posts', buffer, {
114+
headers: { 'Content-Type': 'application/json' }
115+
})
116+
ctx.body = res.data
117+
})
118+
80119
// SSE test endpoint - server side
81120
router.get('/sse-server', async (ctx) => {
82121
ctx.set('Content-Type', 'text/event-stream')
@@ -90,7 +129,9 @@ router.get('/sse-server', async (ctx) => {
90129
let count = 0
91130
const interval = setInterval(() => {
92131
count++
93-
res.write(`event: message\nid: ${count}\ndata: {"count": ${count}, "time": "${new Date().toISOString()}"}\n\n`)
132+
res.write(
133+
`event: message\nid: ${count}\ndata: {"count": ${count}, "time": "${new Date().toISOString()}"}\n\n`
134+
)
94135
if (count >= 5) {
95136
clearInterval(interval)
96137
res.end()
@@ -102,18 +143,18 @@ router.get('/sse-server', async (ctx) => {
102143
router.get('/sse-fetch', async (ctx) => {
103144
// Fetch SSE from our own server
104145
const response = await fetch('http://localhost:3001/sse-server')
105-
146+
106147
// Consume the stream to capture events
107148
const reader = response.body.getReader()
108149
const decoder = new TextDecoder()
109150
let result = ''
110-
151+
111152
while (true) {
112153
const { done, value } = await reader.read()
113154
if (done) break
114155
result += decoder.decode(value, { stream: true })
115156
}
116-
157+
117158
ctx.body = { message: 'SSE stream consumed', data: result }
118159
})
119160

packages/network-debugger/src/core/request.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ function proxyClientRequestFactory(
2222
) {
2323
const actualFn = actualRequest.write
2424
actualRequest.write = (data: any) => {
25-
try {
26-
requestDetail.requestData = JSON.parse(data.toString())
27-
} catch (err) {
28-
requestDetail.requestData = data
25+
// Convert to string at source to avoid IPC serialization issues
26+
// Accumulate multiple writes (e.g., multipart/form-data)
27+
let chunk: string
28+
if (Buffer.isBuffer(data)) {
29+
chunk = data.toString('utf-8')
30+
} else if (typeof data === 'string') {
31+
chunk = data
32+
} else if (data != null) {
33+
chunk = JSON.stringify(data)
34+
} else {
35+
chunk = ''
36+
}
37+
38+
if (requestDetail.requestData) {
39+
requestDetail.requestData += chunk
40+
} else {
41+
requestDetail.requestData = chunk
2942
}
3043

3144
return actualFn.bind(actualRequest)(data)

packages/network-debugger/src/fork/module/network/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ export const networkPlugin = createPlugin('network', ({ devtool, core }) => {
182182
headers: headerPipe.getData(),
183183
initialPriority: 'High',
184184
mixedContentType: 'none',
185-
...(request.requestData
186-
? {
187-
postData: contentType?.includes('application/json')
188-
? JSON.stringify(request.requestData)
189-
: request.requestData
190-
}
191-
: {})
185+
...(request.requestData ? { postData: request.requestData.toString() } : {})
192186
},
193187
timestamp: devtool.timestamp,
194188
wallTime: request.requestStartTime,

0 commit comments

Comments
 (0)