Introduction to Serverless Functions

Introduction to Serverless Functions Accessing Query String Parameters

Learning Paths:
Topics:
Check out a free preview of the full Introduction to Serverless Functions course:
The "Accessing Query String Parameters" Lesson is part of the full, Introduction to Serverless Functions course featured in this preview video. Here's what you'd learn in this lesson:

Jason continues live coding the application by creating an echo function that passes in through an event query string parameters, and renders text. This segment explores the how to filter data using serverless functions.

Get Unlimited Access Now

Transcript from the "Accessing Query String Parameters" Lesson

[00:00:00]
>> This far we have only loaded data with like no parameters, right? So what if I wanna filter? What if I wanna get some data and say well, I wanna send the serverless function something and I want it to return something based on what I sent it. We are able to do that by using a couple ways.

[00:00:20] So the way we've been calling it so far is is with get. So we can use query string parameters. So let's build a quick function that does that. And we're gonna call this echo. So let me set up echo.js. And in echo.js, we're gonna do what we always do, set up a handler, okay?

[00:00:41] And that needs to be an async function. All right, and then down here, we're gonna return StatusCode of 200. And we need to figure out what the body is gonna be. But in here, we need to figure out how to get at some data. So the way that I wanna call this, is I wanna be able to pass in a query string.

[00:01:00] So if I'm looking at my function here, I wanna be able to run .netlify/functions/echo. And I wanna say something like text=hello, right? And I want that to affect, because it's an echo, we just wanted to say that back to us. So how do we do that? The way that we're gonna do that is we're going to use query string parameters, and those get sent in as part of an event.

[00:01:33] So every serverless function is gonna get an event passed into it. The syntax will vary slightly depending on which platform you use. Netlify functions in AWS lambda, this is the syntax that you're gonna use. If you use Azure ,or Google functions, or other providers, the syntax might differ a little bit but the concepts are all the same.

[00:01:55] So you'll have to look up exactly do they call there's a handler or do they call it something else? So they have like a different name per status code. Just make sure that you look that up if you're gonna switch to Azure or Google Cloud Functions or IBM Cloud Functions, I think it's called, just validate.

[00:02:15] But the the concepts that we're talking about today are gonna translate across all serverless platforms. So inside of our event, we are going to get out the title, or the text that we sent in, right? So we wanna get that text value. And that gets sent in, in event.queryStringParameters.

[00:02:34] And so queryStringParameters is just an object that breaks down this. So if I did a bunch of things and, To something like that, then I can console.log my queryStringParameters. And we can see what happens with that. So let me let me actually do that, and then we'll also just echo back, You said: $(text).

[00:03:09] All right, so with that, let's run it. Okay, so our function is running. I'm gonna hit it, I mess something up. And then if we look at our object, what we logged, we can just see that each of those things gets broken into a key and a string value.

[00:03:32] So one thing to note and the reason that I included a number, it's always gonna bring you back a number or a string. So if you need to compare these as strict numbers. You would want to actually convert them to numbers using whatever format you want, percent, or casting it deliberately to a number or whatever your preferred way of doing that is.

[00:03:57] But in general for what we're doing today, we're just gonna use string value, so we kinda don't care. So I can get rid of that. And so now we know we're only looking for this text queryStringParameter, and we're gonna return that. So all of these other ones don't really matter.

[00:04:14] And we've got some text, and we can say whatever we want here. Hooray!