fix-request-body.js 916 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fixRequestBody = void 0;
  4. const querystring = require("querystring");
  5. /**
  6. * Fix proxied body if bodyParser is involved.
  7. */
  8. function fixRequestBody(proxyReq, req) {
  9. const requestBody = req.body;
  10. if (!requestBody) {
  11. return;
  12. }
  13. const contentType = proxyReq.getHeader('Content-Type');
  14. const writeBody = (bodyData) => {
  15. // deepcode ignore ContentLengthInCode: bodyParser fix
  16. proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
  17. proxyReq.write(bodyData);
  18. };
  19. if (contentType && contentType.includes('application/json')) {
  20. writeBody(JSON.stringify(requestBody));
  21. }
  22. if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
  23. writeBody(querystring.stringify(requestBody));
  24. }
  25. }
  26. exports.fixRequestBody = fixRequestBody;