-1

As part of my API proxy, I'd like to map a target resource called search to the proxy resource super-search. So if my base url is myproxy/v1.2, and I create the resource super-search, then I'd like the URLs to be modified as below

myproxy/v1.2/super-search&apikey=123

to

myproxy/v1.2/search&apikey=123

It seems, from reading the documentation here, and this question, that the way to do this would be to simply modify the URL with a JavaScript call out in the pre-flow of the target endpoint by doing

context.targetRequest.url.replace('/super-search', '/search')

Unfortunately though, the URL doesn't seem to include the resource name, so there's nothing to replace.

Alternatively I can do

context.setVariable('target.copy.pathsuffix', false);
context.targetRequest.url += '/search';

but this seems to drop all my query parameters!

How can I change only the resource name on the target without affecting the other elements of the request?

Community
  • 1
  • 1
Tadhg
  • 561
  • 5
  • 15

2 Answers2

0

Try to reset the target URL in Target Pre-flow using a Javascript call-out -

context.setVariable("target.url",context.getVariable("target.url").replace('/super-search', '/search'));
Sudhee
  • 1
  • 2
  • Thanks @Sudhee, but this doesn't work because the target.url does not include the resource name. In this case, the target.url is `myproxy/v1.2` so the replace does nothing – Tadhg Jun 16 '14 at 14:17
0

OK - well I got it working, but it's a nasty hack. It bypasses all the nice logic Apigee surely has to ensure that target URL is written correctly, and I just write it myself in JavaScript

// Disable copying super-search into the output
context.setVariable('target.copy.pathsuffix', false);

//Build the target url manually: root + /search? + params list
var url = context.getVariable('target.url') + "/search?" + context.getVariable('request.querystring');

//Write the target URL back
context.setVariable('target.url', url);

There should be a better way of doing it than this - if you know what it is, please post!

Tadhg
  • 561
  • 5
  • 15