My service dispatches a POST request with some body coming from one service to another. Existing implementation works for GET request:
var rp = require('request-promise');
public pipe(options: Options, req: Request, res: Response): void {
rp(options).pipe(res);
}
I wanted to use it for POST requests as well. Unfortunately it doesn't work when I'm passing a body to options. Method public pipe(options: Options, req: Request, res: Response) is reached, but no further request is coming out of my service. Without body a request is dispatched properly. I'm calling this method like this:
streamFromService(
req: Request,
res: Response,
serviceId: string,
servicePath: string,
query?: URLSearchParams): void {
const forwardHeaders = getForwardHeaders(req);
Promise.resolve(this.serviceLookup.lookup(serviceId, req.headers))
// create URL parameter based on the given method parameters
.then((url) =>
query
? { ...url, pathname: servicePath, search: query.toString() }
: { ...url, pathname: servicePath }
)
// create the URL
.then((url: any) => URL.format(url))
// tap(c => console.log('URL>', c)).
.then((url) => {
req.method.toUpperCase() === 'POST' ?
this.httpClient.pipe(
{method: 'POST', body: req.body, url: url, headers: forwardHeaders},
req,
res
) :
this.httpClient.pipe(
{url: url, headers: forwardHeaders},
req,
res
);
}
)
// tap(console.log).
.tapCatch((err) =>
console.error('Error for ', serviceId, servicePath, err)
);}
What is worth mentioning is that before calling pipe() I'm retrieving some information from req like for mapping headers with req.headers.
Is there a solution for my problem in piping a post request with body?