Accessing query string params in "req" Request objects in Netlify Serverless Functions

Netlify site: https://smsclockwork.netlify.app/hello-fruit, https://smsclockwork.netlify.app/wow-fruit

Hello everyone,

I’m curious about reading query parameters from incoming requests, so I appended some random query parameters to a URL string. My code at the moment doesn’t do anything with them, but they should still be available nonetheless.

Technically, I have access to the entire URL string by accessing the req.url property (I’m not sure how it’s accessible either), but I’ll have to write some custom code to parse out each specific parameter. I noticed in the console-logged req object, there are fields called search and searchParams, but they are not immediately available on the req object; those fields return undefined.

I tried to use the .json() method on the req object, which is a valid method; however, I got the following error: “SyntaxError - Unexpected end of JSON input”.

An interesting observation I noticed is that the context parameter has the params object; however, that object was empty even when additional params were appended to the URL string. This made me wonder: in Netlify Serverless Functions, is the standard to use path parameters as opposed to query parameters? I noticed the documentation seemed to only show examples of path parameters
(ex. netlify.app/:fruit/:flavor)

Here is my Netlify Function code snippet:

import type { Config, Context } from "@netlify/functions";

export default async (req: Request, context: Context) => {
  const data = await req.json();
  console.log('Checking data', data);
  const fruitName = Netlify.env.get("FRUIT_NAME");
  const optionsObject = { status: 200, statusText: `Hello! My fruit is ${fruitName}` };
  return new Response(JSON.stringify(optionsObject));
};

export const config: Config = {
  path: ["/hello-fruit", "/wow-fruit"],
};

In summary, my main questions are the following:

  1. How can I convert the req: Response object into a JSON object, if that’s even possible?
  2. Should I be using path parameters instead of query string parameters? Is this a philosophy of Netlify Serverless Functions I’ll have to adopt?

Thank you very much for any help on this matter!

Have you tried:

new URL(req.url).searchParams.get('<query>')

The Request is a web-standard type, not specific to Netlify. So using that would also be the same as standards, there’s no special philosophy.

Hi again, I was absent for this year, looking forward to your answers.
Why does req.params and req.query not work according to the ‘standard’ linked express lib?
I prefer a shorter notation compared to new URL..
Error is Property 'params' does not exist on type 'Request'.ts(2339)

Express is not a standard. It’s a library. Request is a standard. You can use Express on Netlify: Express on Netlify | Netlify Docs

I don’t understand your statement.

Catching up with @mealthebear, I have 2 query params and want to test it with functions:invoke from terminal.
--querystring a=b&c=d finds a=b with
new URL(req.url).searchParams.get('a')
but not c=d which should work with
new URL(req.url).searchParams.get('c')

What can I do?
Test the function with curl and use config and context as shown above?

I’d need a more details and reliable reproduction than that. If this is something that consistently happens, feel free to provide a repo and I can check further.