Nestjs call POST in controller from GET in another Controller

Viewed 923

I am having API Call, GET from one controller. based on the response, I need to call another Controller POST

I am having issue doing that using redirect(), I am getting "Cannot Get" because the second API call is POST method.

async retrieveOrgUnit(@RequestHeader(HeadersDto) headers: HeadersDto, @Param() params: OrganizationGetUnitDto, @Body() offerDto, @Res() response: Response, cpg: ChargingProvisioningGateway) {
let result = await cpg.organizationUnit.getOrgUnitProfile({
    path: {
        customerExternalId: params.orgId,
        orgUnitExternalId: params.orgId
    },
    body: {}
}).then(async res => {
    res.status
    if( res.data.status === 22){
        **await response.redirect(307, '/offer')** 
      }
    return res.data
  }).catch(err => {
    Logger.log('Failed, Error: ' + err);
    return err.status ? err.status : err.data ? err.data : err
  })

if you could see in response.redirect(307, '/offer') I am calling another controller that is calling POST API, but the method is being GET. any advice how to do that?!

1 Answers

Redirects follow special rules, but generally you cannot change FROM a GET to a different HTTP method, only from other HTTP methods TO a GET. I would suggest just injecting the route's related service class to this current class, and calling the service directly instead of making an HTTP request to your own server and increasing the request timing.

Related