fix-request-body.js 885 B

12345678910111213141516171819202122232425
  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. if (!req.body || !Object.keys(req.body).length) {
  10. return;
  11. }
  12. const contentType = proxyReq.getHeader('Content-Type');
  13. const writeBody = (bodyData) => {
  14. // deepcode ignore ContentLengthInCode: bodyParser fix
  15. proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
  16. proxyReq.write(bodyData);
  17. };
  18. if (contentType && contentType.includes('application/json')) {
  19. writeBody(JSON.stringify(req.body));
  20. }
  21. if (contentType === 'application/x-www-form-urlencoded') {
  22. writeBody(querystring.stringify(req.body));
  23. }
  24. }
  25. exports.fixRequestBody = fixRequestBody;