Skip to content

Issue obtaining body and updating it as part of proxy #942

@coupster74

Description

@coupster74

Checks

Describe the bug (be clear and concise)

I am proxying azure search queries, and would like to obtain the complete request body, modify it, and forward it on. I've tried a number of things, and the results are consistently where either the body is empty, or by the time I get the entire body, the request has already been sent through to the server and cannot be changed.

I've reviewed the recipe here: https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/modify-post.md, (which just replaces the body, a unrealistic usecase), but I don't quite understand why we assume we would have the body at all since it is a stream. I've had to add this within the onProxyReq: to obtain the body:

         req.on('data', (chunk) => {
            bodyData += chunk;
          });

here is the code I currently have which obtains the body successfully, but it is too late to modify it.

 onProxyReq: async (proxyReq, req, res) => {
        if (req.method === 'POST' || req.method === 'PUT') {

          // log the search request
          let bodyData = '';
          Logger.log(req.body);  // is always  undefined

          req.on('data', (chunk) => {
            bodyData += chunk;
          });
          req.on('end', async () => {
            const email = req.user ? req.user : 'unknown';
            let searchArgs = {};
            try {
              // bodyData exists
              searchArgs = JSON.parse(bodyData);
              // log this..
              if (searchArgs['search']) {
                const activityLogService = app.get(ActivityLogService);
                await activityLogService.logSearchActivity(searchArgs, email);
              }
            } catch (e) {
              // parse error
              Logger.log('error', e);
            }
          });
        }
      },

what am I missing here?

Step-by-step reproduction instructions

1. ...
2. ...

Expected behavior (be clear and concise)

Within the onProxyReq there would be an approach to obtain, modify and forward on the req.body successfully

How is http-proxy-middleware used in your project?

to proxy requests through to azure search. Specifically
- ensure they are authenticated (via jwt token) with my middleware, 
- add the azure search APIKEY (removing the need to include it from the front end, protecting the key)
- log the search request details
- ideally (the issue here), modify the search based on the user's profile (from the jwt token)

What http-proxy-middleware configuration are you using?

don't understand the question

What OS/version and node/version are you seeing the problem?

System:
    OS: macOS 13.5.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.54 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.19.4 
  Managers:
    CocoaPods: 1.11.2 - /usr/local/bin/pod
    Homebrew: 4.1.4 - /opt/homebrew/bin/brew
    pip3: 23.0.1 - /opt/homebrew/bin/pip3
    RubyGems: 3.0.3.1 - /usr/bin/gem
  Utilities:
    CMake: 3.26.4 - /opt/homebrew/bin/cmake
    Make: 3.81 - /usr/bin/make
    GCC: 14.0.3 - /usr/bin/gcc
    Git: 2.41.0 - /opt/homebrew/bin/git
    Clang: 14.0.3 - /usr/bin/clang
    Subversion: 1.14.2 - /opt/homebrew/bin/svn
    Curl: 8.1.2 - /usr/bin/curl
  Servers:
    Apache: 2.4.56 - /usr/sbin/apachectl
  Virtualization:
    Docker: 24.0.2 - /opt/homebrew/bin/docker
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4
  IDEs:
    Android Studio: 2021.1 AI-211.7628.21.2111.8193401
    Atom: 1.60.0
    VSCode: 1.82.0 - /opt/homebrew/bin/code
    Vim: 9.0 - /usr/bin/vim
    Xcode: 14.3.1/14E300c - /usr/bin/xcodebuild
  Languages:
    Bash: 3.2.57 - /bin/bash
    Perl: 5.30.3 - /usr/bin/perl
    Protoc: 23.2 - /opt/homebrew/bin/protoc
    Python: 3.8.11 - /opt/homebrew/Caskroom/miniconda/base/bin/python
    Python3: 3.11.4 - /opt/homebrew/bin/python3
    Ruby: 2.6.10 - /usr/bin/ruby
  Databases:
    PostgreSQL: 14.8 - /opt/homebrew/bin/postgres
    SQLite: 3.39.5 - /usr/bin/sqlite3
  Browsers:
    Chrome: 116.0.5845.187
    Safari: 16.6

Additional context (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions