Skip to content

Commit 8c87390

Browse files
authored
fix(http-crawler): do not hang on POST without payload (#1546)
1 parent 19cdf13 commit 8c87390

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/http-crawler/src/internals/http-crawler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ export class HttpCrawler<Context extends InternalHttpCrawlingContext<any, any, H
641641
};
642642
}
643643

644-
if (/PATCH|POST|PUT/.test(request.method)) requestOptions.body = request.payload;
644+
if (/PATCH|POST|PUT/.test(request.method)) requestOptions.body = request.payload ?? '';
645645

646646
return requestOptions;
647647
}

test/core/crawlers/http_crawler.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ router.set('/redirectWithoutCookies', (req, res) => {
4141
res.end();
4242
});
4343

44+
router.set('/echo', (req, res) => {
45+
res.setHeader('content-type', 'text/html');
46+
req.pipe(res);
47+
});
48+
4449
let server: http.Server;
4550
let url: string;
4651

@@ -231,3 +236,23 @@ test('no empty cookie header', async () => {
231236

232237
expect(results).toStrictEqual([]);
233238
});
239+
240+
test('POST with undefined (empty) payload', async () => {
241+
const results: string[] = [];
242+
243+
const crawler = new HttpCrawler({
244+
handlePageFunction: async ({ body }) => {
245+
results.push(body.toString());
246+
},
247+
});
248+
249+
await crawler.run([
250+
{
251+
url: `${url}/echo`,
252+
payload: undefined,
253+
method: 'POST',
254+
},
255+
]);
256+
257+
expect(results).toStrictEqual(['']);
258+
});

0 commit comments

Comments
 (0)