Skip to content

Commit 3c87f97

Browse files
authored
Merge pull request #24 from stegano/feature/add-http-method
fix: add methods that can include the request body
2 parents b3da02c + c27b99d commit 3c87f97

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ const httpProxyMiddleware = async (
4646
if (pathRewrite) {
4747
req.url = rewritePath(req.url as string, pathRewrite);
4848
}
49-
if (["POST", "PUT", "PATCH"].indexOf(req.method as string) >= 0 && typeof req.body === "object") {
49+
/**
50+
* Please refer to the following links for the specification document for HTTP.
51+
* @see https://tools.ietf.org/html/rfc7231
52+
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
53+
*/
54+
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];
55+
if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "object") {
5056
req.body = JSON.stringify(req.body);
5157
}
5258
proxy
5359
.once("proxyReq", ((proxyReq: any, req: any): void => {
54-
if (["POST", "PUT", "PATCH"].indexOf(req.method as string) >= 0 && typeof req.body === "string") {
60+
if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "string") {
5561
proxyReq.write(req.body);
5662
proxyReq.end();
5763
}

0 commit comments

Comments
 (0)