Is there an easy way to get the query string param...
# help
k
Is there an easy way to get the query string params sent to a SvelteKit endpoint? Looks like I can get the full URL as shown below, but I don't see an easy way to get the query string params.
Copy code
ts
export async function post(event: RequestEvent): Promise<RequestHandlerOutput> {
  console.log(event.request.url);
}
Example: If the request is sent to
http://localhost:3000/api/my-endpoint?test=abc123
, I want to access the value of
test
, which would be
abc123
. Thanks for any help you can give!
n
Hello @Kellen Mace! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
l
the post handler in sveltekit should look like this:
Copy code
js
export async function post({ request }) {
so you can get the search params from the request like so:
Copy code
javascript
  const url = new URL(request.url);
  const search = url.searchParams.get("test");
n
Kellen Mace (2022-03-16)
l
iirc sveltekit should give you browser request objects, so mdn is a great resource
k
Ah! Thanks so much!
Yeah, I was reading MDN's page on
Request
and I'm familiar with
URLSearchParams
too, but not instantiating
URL
, like you did
Thanks for the help, @User! I really appreciate it 🙌