Search code examples
urlparametersgetdenoopine

Get url parameters in Deno using Opine or other Deno Library


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


Solution

  • 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,
    });