Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node HTTP: Handling Request Body Conversion #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/node-http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ function getRequestBody(req: IncomingMessageWithBody): Promise<string> {
// add `body` to request for downstream readers
req.body = Buffer.concat(buffer);

// resolve promise
resolve(req.body.toString());
// Convert Buffer to a string
const contentType = req.headers['content-type'] || '';

if (contentType.includes('application/json')) {
req.body = JSON.parse(req.body.toString());
} else if (contentType.includes('application/x-www-form-urlencoded')) {
req.body = req.body.toString(); // Keep it as a string for form submissions
} else {
req.body = req.body.toString(); // Default to string conversion
}

resolve(req.body); // Resolve with the properly formatted body
};

const onErr = (err: Error) => {
Expand Down