# How to override default URL on Edge Micro Gateway?

**URL:** <https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [January 19, 2016, 4:10pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456 "2016-01-19T16:10:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 19, 2016, 4:10pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/1 "2016-01-19T16:10:09Z")

</div>

I have one MicroGateway aware proxy defined and downloaded in to Edge MicroGateway with these defaults:

```auto
proxies:
  - max_connections: -1
    name: edgemicro_weather
    revision: '1'
    proxy_name: default
    base_path: /weather
    target_name: default
    url: 'https://weather.yahooapis.com'

```

Let’s say for some business reason based on payload condition I want to override the target URL from **[https://weather.yahooapis.com](https://weather.yahooapis.com)** to **[https://legacy.weather.yahooapis.com](https://legacy.weather.yahooapis.com).**

**How do I dynamically create backend URL which overrides the default downloaded one?**

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 1:44pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/2 "2016-01-27T13:44:08Z")

</div>

I’ve done some digging into this myself. I found that the target URL lives in `res.proxy.parsedUrl`, and it can be manipulated prior to the request being made. This URL is used as a base, to which the request path (if any, outside of the request basepath) and request querystring are appended. The code used to build the final target URL looks like this (it lives in /gateway/lib/main.js, at least in version 1.1.1):

```auto
var target_path =
    proxy.parsedUrl.pathname +
    reqUrl.pathname.substr(proxy.basePathLength, reqUrl.pathname.length); // strip leading base_path
if (reqUrl.search) target_path += reqUrl.search;
target_path = target_path.replace(double_slash_regex, '/');

var treqOptions = {
    hostname: proxy.parsedUrl.hostname,
    port: proxy.parsedUrl.port,
    path: target_path,
    method: req.method,
    headers: target_headers, // pass through the modified headers
    agent: proxy.agent
};

```

So, in order to manipulate the target url, you might do something like this from within a plugin (keep in mind that this needs to run in the request flow):

```auto
res.proxy.parsedUrl.hostname = "dynamic-hostname.com";
res.proxy.parsedUrl.pathname = "/dynamic/path";
reqUrl.pathname = res.proxy.base_path + '/more/dynamic/path'; // It is important that `proxy.base_path` remains at the beginning of `reqUrl.pathname` 
reqUrl.search = '?dynamic=querystring';

```

Keep in mind that, as far as I can tell, none of this is explicitly documented. It could be subject to change in future versions, and there may even be more straightforward ways of achieving this.

In fact, I really hope someone can correct me and tell me a better way of achieving this, because the method I outlined above is very ugly. Why should you need to manipulate both the request and response objects just to have full dynamic control of the target URL? Very ugly indeed.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 5:30pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/3 "2016-01-27T17:30:01Z")

</div>

What you have suggested is indeed a custom plugin. Since you have it figured out, how about you pushing your entire plugin code somewhere ( on github may be) so that others can benefit.

I think pretty much everything would work but Analytics that you would see on Edge might refer to the request which Edgemicro received and will not show dynamically added paths.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 5:52pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/4 "2016-01-27T17:52:52Z")

</div>

Agreed [@epackwood](https://community.apigee.com/users/908/epackwood.html) ugly… also seems that you you may want to abstract away some of this in-case the core API changes. I’d hate to take this approach with a bunch of APIs then have the ‘res.proxy.parsedUrl’ go away and starts breaking stuff.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 5:55pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/5 "2016-01-27T17:55:17Z")

</div>

Speaking of GitHub, I think we can look into how to share this with the larger community. But if we show you ours… we’d want to see yours… Is there a GitHub repo for Edge Micro we can monitor, to create issues and possibly contribute to?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 5:56pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/6 "2016-01-27T17:56:32Z")

</div>

Yes, that’s a good point and is a work in progress. BTW, Edgemicro being a node.js based impl, you already have access to all of its source including plugins. 🙂

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 6:05pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/7 "2016-01-27T18:05:38Z")

</div>

[@prabhat](https://community.apigee.com/users/150/prabhatapigeecom.html) Good point about the analytics. I’m also curious, is this a use-case that’s not intended for the microgateway? I’m always a little hesitant to rely on undocumented implementation details like this, as (as [@Kristopher Kleva](https://community.apigee.com/users/188/kklevallbeancom.html) mentioned) you never know when they might change out from under you.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 6:06pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/8 "2016-01-27T18:06:18Z")

</div>

Right. But would be awesome to be able to fork, create issues and create pull requests.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 27, 2016, 6:51pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/9 "2016-01-27T18:51:01Z")

</div>

+1 and totally agreed.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 28, 2016, 4:10pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/10 "2016-01-28T16:10:04Z")

</div>

[@epackwood](https://community.apigee.com/users/908/epackwood.html) Thanks! However, the suggested solution appears to work only with **onrequest** event in the request flow. How can we get it to work with a **onend\_request** event?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 28, 2016, 4:36pm UTC](https://discuss.google.dev/t/how-to-override-default-url-on-edge-micro-gateway/16456/11 "2016-01-28T16:36:57Z")

</div>

I’m not sure. I have run into this sort of issue with `onend_request` as well. I found that at this point, it seems to be too late to alter the target request. Since the documentation suggests that this is the point when all data has been received from the client, it would definitely be advantageous to be able to alter the request there. E.g. by taking a property from a JSON payload, and moving it into a header, or determining aspects of the target URL based on POSTed form data.
