-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,42 @@ http.createServer(function (req, res) { | |
|
||
**[Back to top](#table-of-contents)** | ||
|
||
#### Forward POST requests with a json body | ||
|
||
```ts | ||
const proxy = httpProxy.createProxyServer({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the 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 |
||
target: `http://localhost:3002` | ||
}) | ||
|
||
proxy.on('proxyReq', (proxyReq, req, res, options) => { | ||
// @ts-expect-error | ||
if (req.body) { | ||
// @ts-expect-error | ||
let bodyData = req.rawBody | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsafe access to req.rawBody |
||
proxyReq.setHeader('Content-Type', 'application/json') | ||
// proxyReq.setHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)) | ||
|
||
proxyReq.write(bodyData) | ||
} | ||
}) | ||
|
||
|
||
``` | ||
then somewhere in your HTTP handler | ||
```ts | ||
proxy.web( | ||
req, | ||
res, | ||
{ | ||
timeout: 1000 * 60 * 5 // 5 minutes | ||
}, | ||
next | ||
) | ||
``` | ||
|
||
**[Back to top](#table-of-contents)** | ||
|
||
#### Using HTTPS | ||
You can activate the validation of a secure SSL certificate to the target connection (avoid self-signed certs), just set `secure: true` in the options. | ||
|
||
|
There was a problem hiding this comment.
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