How to get headers and IP address of HTTP requests?

Hi, I’d like to create an endpoint collect-headers and get some info of the HTTP header for every request. Something like this:

const Routes = () => {
  return (
    <Router>
      <Route path="/collect-headers" page={CollectHeadersPage} name="collectHeaders" />
      ...
    </Router>
  )
}

This is the info I want to collect in CollectHeadersPage:

  • IP address of the client.
  • Info of the user agent, for example:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Is this possible using Redwood?
I deploy in AWS lambda using serverless, but I’d prefer to use a Redwood core functionality instead of something specific of AWS or creating a 3rd party solution (like an Express endpoint to do this).

Am not near my laptop for a concrete example, but you can access these within any service on the context— not the global context but the context that is an argument to the service.

AWS lambda will populate the ipAddress for each request.

I believe it and user agent is on the requestContext: redwood/apiFunction.ts at 98b1ee7f51291a5c3e005cb75742b6e3d0eb9766 · redwoodjs/redwood · GitHub

Hi @dthyresson , thank for reply. I’ll take a look for service contexts (not sure where to find that info though, the Docs are not very clear regarding request contexts).

AWS lambda will populate the ipAddress for each request.

Is it possible to test/debug this feature in a local dev environment?
Sorry for silly basic questions.

Thanks

Not a silly question at all! In fact being able to test functions and webhooks is a big part of the testing suite as it makes thing much easier to … well … test :slightly_smiling_face:

Try the mockHttpRequest as shown in Serverless Functions | RedwoodJS Docs

1 Like