I'm using Deno and Opine to create an API, how to get the data from the parameters sent via the url Get method? It can be with Opine or another library. Parameters i want to get: access_token, refresh_token, token_type Here is an example url: http://localhost:80/auth/confirmation#access_token=QxsDVgs7OqTrS6_7wuIuQNfE&expires_in=3600&refresh_token=XcaYQFJNfgbnsW3BJjY1ug&token_type=bearer&type=signup
Asked
Active
Viewed 116 times
-1
-
Does this answer your question? [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – jsejcksn Sep 13 '22 at 16:01
-
i found an example i this tutorial: https://medium.com/deno-the-complete-reference/process-query-params-in-deno-6f71caa7933b – Gouveia Sep 13 '22 at 18:36
-
but for me it does not work, becouse in my url i have # and not ? before the parameters, so when i try to make searchParams does not work. example: const reqUrl = new URL(req.url); console.log(reUrl.searchParams.get('acess_token')) – Gouveia Sep 13 '22 at 18:37
1 Answers
1
Fragment identifiers are not sent to the server by the browser. You must parse the fragment identifier in the browser client and send it to the server if you need it there (e.g. in a POST request). You can parse the kind of format you’ve shown using this technique:
Refs:
// const url = new URL(location.href);
const url = new URL('http://localhost/auth/confirmation#access_token=QxsDVgs7OqTrS6_7wuIuQNfE&expires_in=3600&refresh_token=XcaYQFJNfgbnsW3BJjY1ug&token_type=bearer&type=signup');
const fragment = url.hash.slice(1);
const params = new URLSearchParams(fragment);
const accessToken = params.get('access_token');
const expiresIn = params.get('expires_in');
const refreshToken = params.get('refresh_token');
const tokenType = params.get('token_type');
const type = params.get('type');
console.log({
accessToken,
expiresIn,
refreshToken,
tokenType,
type,
});

jsejcksn
- 27,667
- 4
- 38
- 62