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

add a showcase how to proxy POST with a body #1629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

capaj
Copy link

@capaj capaj commented Mar 7, 2023

props to @Tokscull

@alanhe421
Copy link

so any update

// @ts-expect-error
if (req.body) {
// @ts-expect-error
let bodyData = req.rawBody
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe access to req.rawBody

@@ -228,6 +228,42 @@ http.createServer(function (req, res) {

**[Back to top](#table-of-contents)**

#### Forward POST requests with a json body

```ts
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { IncomingMessage, ServerResponse } from 'http'; import * as httpProxy from 'http-proxy';

add this to the first

#### Forward POST requests with a json body

```ts
const proxy = httpProxy.createProxyServer({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the const proxy = httpProxy.createProxyServer create an interface interface ExtendedIncomingMessage extends IncomingMessage { body?: any; rawBody?: Buffer | string; }

the reason to create this interface is to typeScript will catch errors if you try to access properties that don't exist. and to make it runtime error free

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants