How to change the target API URL in an Edge Microgateway custom plugin?

Depending on the request payload, I want to be able to route the API call to another target API URL (for example to implement data-sharding). Overloading req.url in the original onrequest() does not seem to be working. Do I mis something? Is there a way to implement this manual routing?

0 10 1,246
10 REPLIES 10

Not applicable

@Francois-Xavier KOWALSKI I believe you can override req.url for the path and query parameters and override req.headers.host to change the hostname.

EDIT: It looks like the microgateway is using the original target URL, regardless of any changes made to the Host header.

See: https://github.com/apigee/microgateway-core/blob/master/lib/plugins-middleware.js#L186-L193

I haven't tested it out, but you may be able to alter res.proxy.parsedUrl.hostname in a plugin to change the host.

And it looks like you'd have to modify req.reqUrl to change the path.

Hello @kevinswiber, I just tested, but it does not seem to work: when calling next() with a modified re.headers.host & req.url, the target URL that EMG tries to reach is left un-modified. Not using next() & implementing my on routing seems to work... but is it compatible with the EMG design intent?

var targetOptions = {
  method: req.method,
  uri: targetUrl,
  headers: req.headers
};
req.pipe(request(targetOptions)).pipe(res);

Hey, @Francois-Xavier KOWALSKI. I found that a little surprising, but it looks like you're correct! I updated my response with another suggestion. Disclaimer: I haven't tried it out myself yet, and I'm aware that it looks like a bit of a hack.

Hi @kevinswiber thanks for the pointer! I would indeed not feel very comfortable in manipulating those fields directly... that looks nowhere close to a public/stable API to play with 😉

Would you see any reason (like "violating the EMG design intent because of blah...") for me to NOT use the double-pipe snippet above?

Not applicable

Hello @Francois-Xavier KOWALSKI, We are implementing something similar of modifying the target url in EMG. Did you implement the above in onrequest event handler ?

Yes. I initially thought it would work only in onrequest_end(), but it works in onrequest().

Not applicable

@Francois-Xavier KOWALSKI

Microgateway version 2.2.3-beta now supports this.
You may override one or both of

req.targetHostname

req.targetPath

See the release notes for the other changes.

hi @mfierro , @Francois-Xavier KOWALSKI

We have a similar requirement where the target url would be sent as a header value.

How do we assign the header value to res.proxy.parsedUrl.hostname? or req.targetHostname?

how are these both different? res.proxy.parsedUrl.hostname and req.targetHostname?

We are running on 2.2.4-beta


Hello,

I am running version 3.0.3 and this still doesn't seem to work. In my onend_request function, I override the following variables:

req.targetPath

req.url

req.reqUrl.path

req.reqUrl.pathname

req.reqUrl.href

None of these seem to change the target endpoint. As an aside, I'm also curious about whether it is possible to send out a POST request as a result of a GET request (Required for REST to SOAP). Modifying req.method doesn't work. Anyone had any luck with this (without changing the enderlying plugin routing implementation)?

req.targetHostname is the thing you use to override the target hostname.