In an Apigee API Proxy, why does rewriting "target.url" via a JavaScript policy not route to the new target? - apigee

The use case here is that the target endpoint URL has to be changed based on some conditions. The JavaScript policy used to overwrite "target.url" does not get the call routed to the new target. It still routes to the 'Default Target Endpoint URL' set on the 'Overview' tab of the API Proxy.
Here is the JavaScript:
context.setVariable("target.url", url);
where URL is the new target URL value that needs to be set.
http://apigee.com/docs/api-services/content/javascript-object-model shows a similar example and http://apigee.com/docs/api-services/api/variables-reference confirms that the variable reference is correct.

Please ensure the policy for the JavaScript is in the Targets side of the flow (vs. Proxies side). You won't see the change take in effect if the policy is executing in the Proxies side of the flow.

Related

Can we use a variable which has endpoint URL directly in HTTP Request UIPath Activity?

How to use a variable directly in Http Request Endpoint?
If you close the activity configuration Wizard, your activity will still be there and you can fine tune the configuration freely in the Properties panel. In your case, configuring the Endpoint by using a variable.
Example

Apigee rest endpoint path mapping to custom path

I have rest end point /admn_resource_manager.I have created a apigee proxy to expose this.
I dont want to expose it like this to others as I want something like /adminmanager.
Is there any way to map /adminmanager to /admn_resource_manager using Apigee.
end user would use http://someurl.apigee/adminmanager instead of http://someurl.apigee/admn_resource_manager
I explored KeyValueMapoperation and AssignMessage in Apigee.
I am not sure if these are the right option to implement map path.I didn't get any example for this either.
The way you would think to do this would be to use the Assign Message policy and use the Set -> Path element. But this policy isn't currently working as designed for rewriting the proxy's target URL. See the Assign Message Guidance for more details.
To rewrite the incoming URL to a different target URL you can use the Assign Message Policy to set the entire URL (target.url) in the Target Endpoint flow, or you can use a JavaScript callout to set it. I chose to use a JavaScript callout because it gives a lot more control when rewriting the URL.
Here is an example project on Github I put together for this you can use to see how I did it. It uses the swapi.co api as the target endpoint. This proxy uses the Assign Message and JavaScript callout policies to rewrite the URL. Here's some details about it...
Proxy Endpoints
Create a proxy endpoint for each resource you are renaming.
This is where you setup the Assign Message policy to set the variables for the new path suffix.
Assign Message Policies
Set on the PreFlow of each proxy endpoint to set the targetPathSuffix and appendResourceIdToUrl (if needed) variables.
JavaScript Policy
Calls out to the URLRewrite.js file to execute the js code.
Set on the TargetEndpoints PreFlow and executes on each request
Uses the variables set in the Assign Message Policies to change the target.url variable.
I think Apigee can do it.
When I was started Apigee I have learned and try to understand from the picture below. (I think it is describe the main concept of this platform)
From your scenario,
You can specify the URL that you wants client to call maybe someurl.apigee/adminmanager or something else
Apigee is a middle also known as a Gateway. When you received the request from client, you can manage whatever you want. Of course, including pass your client to other URL like someurl.apigee/admn_resource_manager . (You just assigned new url to that request)
Because I'm not an expert as well so, you this link below can explain you more information.
Link:Using Flow Variables

Google Tag Manager 403's every request even if CORS mapping is defined

when I moved to AMP, the Google Tag Manager stopped to working.
The problem occurs every time when I open my AMPed page, I can see some errors in browser console, e.g.
First error:
https://www.googletagmanager.com/amp.json?id=MY_GTM_TAG&gtm.url=MY_HTTP_URL
(403)
Second error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
In my class that extends WebMvcConfigurerAdapter I overwritten the method addCorsMappings like this:
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(true);
};
But it still doesn't work (this method is executed on startup, I checked it). Do you have any ideas / tips why?
EDIT 1 (22.12.2016):
Q: How are you loading tag manager? Are you using the AMP version of the script? (#Jim Jeffries)
A: Yes, in <head> I included the following piece of code:
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
and in <body> there is:
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=${googleTagId}&gtm.url=SOURCE_URL" data-credentials="include"></amp-analytics>
I was having the same issue and it turns out you can't use your old GTM "Web" container for this so you'll have to create a specific AMP Container.
As per Google's instructions found here:
Create an AMP container
Tag Manager features an AMP container type. Create a new AMP container for your project:
On the Accounts screen, click More Actions (More) for the account
you'd like to use. Select Create Container.
Name the container. Use a descriptive name, e.g. "example.com - news - AMP".
Under "Where to Use Container", select AMP.
Click "Create".
Based from this thread, maybe you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about how to achieve that is Using CORS.
*When you are using postman they are not restricted by this policy. Quoted from Cross-Origin XMLHttpRequest:*
Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.
Also based from this forum, the app must authenticate as a full admin and POST the desired CORS configuration to /rest/system/config.

Play 2 javascript routing with https

I'm using play's javascript routing (http://www.playframework.com/documentation/2.2.x/ScalaJavascriptRouting). I have a nginx front-end server, which I configured to force https.
When I try to make an ajax call, I get an error, since the javascript routing generates http urls and my browser blocks them because of cross origin access.
How can I configure the javascript routing to serve https urls?
How are you making the calls? The Play JavaScript router returns objects with two properties, one is a method property which contains the method, eg POST, GET etc. The other is a url property which contains the relative URL.
There are also two methods on the returned object, one called absoluteURL, the other called webSocketURL. These will return absolute URLs according the hostname of the current page, and they will be http or https (or ws or wss) based on whether the current page is secure or not.
So, if using with jQuery for example, you should be able to just:
$.ajax(jsRoutes.controllers.MyController.myAction("foo")).then(...)
And it will use the relative URL, or you can also:
var route = jsRoutes.controllers.MyController.myAction("foo")
$.ajax({
method: route.method,
url: route.url,
...
}).then(...)
If you're trying to make that requests using absolute urls, Hostnames, protocols and ports should match because of same-origin policy. You have at least two options:
Use relative urls on ajax requests like href="/your/page"
Use the protocol relative url's. Example: href="//your.domain.com/your/page.
Here is a detailed article by Paul Irish about protocol relative url's:
http://www.paulirish.com/2010/the-protocol-relative-url/
You have to pass it as a parameter to absoluteURL/websocketURL.
Frome Chrome dev tools:
> jsRoutes.controllers.CalendarController.getEvents().absoluteURL
function (s){return _s('http',s)+'localhost:9000'+r.url}
> jsRoutes.controllers.CalendarController.getEvents().absoluteURL()
"http://localhost:9000/events"
> jsRoutes.controllers.CalendarController.getEvents().absoluteURL(true)
"https://localhost:9000/events"
> jsRoutes.controllers.CalendarController.getEvents().webSocketURL
function (s){return _s('ws',s)+'localhost:9000'+r.url}
> jsRoutes.controllers.CalendarController.getEvents().webSocketURL()
"ws://localhost:9000/events"
> jsRoutes.controllers.CalendarController.getEvents().webSocketURL(true)
"wss://localhost:9000/events"
_s is defined at https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/routing/JavascriptReverseRouter.scala#L54 as
function(p,s){return p+((s===true||(s&&s.secure))?'s':'')+'://'}

Seaside - how to get the full url while in a session

I am developing an application that will serve multiple customer-organizations, each of them should be given access based on a fixed url. Example: domain/myapp/CustomerOrg1
Previously I always registered a new WAComponent-subclass for each of these entry-points. That does work but there has to be a better solution, I would like a single component-class to find out which URL the request uses (to then respond with the customer-org's homepage)
I tried:
registering a WARequestHandler-subclass; and it allows me to find out the full path (incl. /CustomerOrg1) but I am outside of any session and don't know how to get into one.
registering a WAComponent-subclass as /myapp, and it works in that it also handles /myapp/CustomerOrg1 automatically, however when I try to find out the URL used (by self session url inspect) it claims to be only the base-url (/myapp).
Try
self requestContext request uri
and if you are not in a component but any object you can do
WACurrentRequestContext value request uri
Please be aware that the uri you get in the answer by Norbert is in a production environment a value that has already been processed, and possibly modified, by your (Apache/nginx/etc) webserver responsible for static content and load balancing.

Resources