Skip to content

Commit

Permalink
Try/catch when reading request body
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Jun 12, 2024
1 parent 709c211 commit 4f70d7d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/msw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,18 @@ export default class MswConfig {
request.headers?.get('content-type')?.toLowerCase() || '';
const hasJsonContent = contentType.includes('json');
if (hasJsonContent) {
requestBody = JSON.stringify(await request.json());
try {
requestBody = JSON.stringify(await request.json());
} catch (e: unknown) {
// Just because a content-type is set, doesn't mean it will actually have a body
}
} else {
// This will parse multipart as text, which I think will work? Should be tested
requestBody = await request.text();
try {
requestBody = await request.text();
} catch (e: unknown) {
// Just because a content-type is set, doesn't mean it will actually have a body
}
}
const requestHeaders: Record<string, string> = {};
request.headers.forEach((v, k) => {
Expand Down

0 comments on commit 4f70d7d

Please sign in to comment.