Search code examples
lambdanetlifyquery-stringnetlify-function

Netlify lambda function query string parameters are passed in different way between local environment and production environment


I created several lambda functions and deployed them on the Netlify service.

I passed several queries via GET request, locally they passed as an array but once it is deployed they passed as a string.

lambda function side:

export const handler = constructNetlifyHandler({
  get: async (event, context, postgrest) => {
    console.log(event.queryStringParameters);

    return await getAllTasks(
        postgrest,
        event.queryStringParameters,
        event.loggedInUserOrg
    );
  },
});

postman side:

{{host}}/tasks?orgUid=06ea7872-32eb-4e7a-ba45-63e2b9e6c747&statusUid=ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9&statusUid=78dcdbe1-007a-493a-ad94-50a0ec613d0d&statusUid=1cbc65b8-831d-4cba-a1ad-111a0757e76b

Logs on local environment:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: [
    'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
    '78dcdbe1-007a-493a-ad94-50a0ec613d0d',
    '1cbc65b8-831d-4cba-a1ad-111a0757e76b'
  ]
}

Logs on netlify production envionment:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: 'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9, 78dcdbe1-007a-493a-ad94-50a0ec613d0d, 1cbc65b8-831d-4cba-a1ad-111a0757e76b'
}

Expected behavior

In both environments, query parameters are passed in the same way. (as an array of strings or as a string)

Screenshots

Postman side:

image

Logs on local environment:

image

Logs on production environment:

image

Run the following command npx envinfo --system --binaries --npmPackages netlify-lambda and attach the output

 System:
    OS: macOS 11.2.3
    CPU: (4) x64 Intel(R) Core(TM) i3-9100 CPU @ 3.60GHz
    Memory: 54.82 MB / 24.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.10 - ~/.nvm/versions/node/v12.20.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  npmPackages:
    netlify-lambda: ^2.0.3 => 2.0.3 

Solution

  • Solution 1: Do the matching inside your function

    Since Netlify redirect mechanism is not able to provide you the data of which rule it matched, you could try to match the original request in your function to determine what it should do.

    Example:

    _redirects

    > /first-thing /.netlify/functions/the-only-function 200! /second-thing
    > /.netlify/functions/the-only-function 200! 
    

    the-only-function.js

    exports.handler = async function(event, context) {
        if (event.path === "/first-thing") {
            // do one thing
        } else if (event.path === "/second-thing") {
            // do the other thing
        } else {
            // return 400 status, because the function was invoked raw
        }
    }
    

    Solution 2: Proxy headers hack

    When you rewrite something you can add custom proxy headers (click for docs) 1.

    The same example solved using those:

    netlify.toml

    [[redirects]]
    from = "/first-thing"
    to = "/.netlify/functions/the-only-function"
    status = 200
    force = true
    headers = {X-Variant = "first-thing"}
    
    [[redirects]]
    from = "/second-thing"
    to = "/.netlify/functions/the-only-function"
    status = 200
    force = true
    headers = {X-Variant = "second-thing"}
    

    the-only-function.js

    exports.handler = async function(event, context) {
        if (event.headers["X-Variant"] === "first-thing") {
            // do one thing
        } else if (event.headers["X-Variant"] === "second-thing") {
            // do the other thing
        } else {
            // return 400 status, because the function was invoked raw
        }
    }
    

    Hope this helps you solve your specific issue!

    Here is the reference