Search code examples
node.jstypescriptrequestnestjsdecorator

How to get full URL from a Query in a request?


I'm trying to get a full URL from a query parameters in a GET request. I'm working in a NestJS project.

Let me explain it. I've this example URL:

http://127.0.0.1:4000/seni/abc/new-url?url=new://way?si=asdas@direction&pn=(null)&mc=7&tr=HD&tp=Pay&am=17&mam=17&cu=INR

I've tried to build my controller with the follow decorators:

@Query() query -> Create an object with diferent variables like:

query: {
    url: new://way?si=asdas@direction,
    pn: null,
    mc: 7,
    tr: HD,
    tp: Pay,
    am: 17,
    nam: 17,
    cu: INR
}

@Query('url') -> Only return url=new://way?si=asdas@direction

Do you know if there is a way to get the full URL? fullUrl = new://way?si=asdas@direction&pn=(null)&mc=7&tr=HD&tp=Pay&am=17&mam=17&cu=INR

I've thought to create a new decorator get the full URL and split it. But I would like to know if there is a better way.


Solution

  • The new URL also includes a query with several parameters delimited by &, so it's parsed, and they are perceived as additional parameters in the original URL.

    Instead, you should encode the new URL in the original query:
    http://127.0.0.1:4000/seni/abc/new-url?url=new%3A%2F%2Fway%3Fsi%3Dasdas%40direction%26pn%3D%28null%29%26mc%3D7%26tr%3DHD%26tp%3DPay%26am%3D17%26mam%3D17%26cu%3DINR

    And decode it in your code:

    newUrl = decodeURI(query.url);