Home > database >  How to get URL query withouth parsing it in NestJS
How to get URL query withouth parsing it in NestJS

Time:11-05

Hello everyone I am trying to just get my route with all query params as it is, withouth parsing it. My route is something like this:

http://somewebsite/orders?key1=value1&key=value

I don't want to use

@Query()

that returns the object with key/value pairs I just want to get plain string value of everything that is behind ? so I want to get something like this

string = "key1=value1&key=value"

edit

controller.ts

@Get('/orders')
getOrders(
    @Query(ValidateQueryPipe) query: QueryParameters): Subscription {
    // here I want to have my query as a string not as an object. 
    })
}

so when I send a request to my route from let's say postman I will be able to have all the key/value pairs I pass but as a string...

Thanks

CodePudding user response:

Try to pass the request object to your function and get the originalUrl, where the query string should be located

import { Req } from '@nestjs/common';
import { Request } from 'express';

getOrders(@Req() request: Request): Subscription {
  const regex = /(?<=\?).*$/gm;
  const result = request.originalUrl.match(regex);

  if (result) {
    const query = result[0];
  }
}
  • Related