trying to add .json to end of all request

Hi All, I am very new to Apigee. I am trying to add .json to end of all request when I am trying call proxy endpoint apigee.net/products it is calling target endpoint as https:xyz/products.json but when trying to call apigee.net/products/1 the target endpoint is translating to https:xyz/products.json/1 but I am expecting https:xyz/products/1.json The code I am using to use is

<TargetEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request/> 
    <Response/> 
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request> 
      <Step> 
        <Name>JavaScript-1</Name> 
      </Step> 
    </Request> 
    <Response/> 
  </PostFlow> 
  <Flows/> 
  <HTTPTargetConnection> 
    <Properties/> 
    <URL>https:xyz/products</URL> 
  </HTTPTargetConnection> 
</TargetEndpoint>

JavaScript-1

var targeturl = context.getVariable('target.url'); 
context.setVariable('target.url', targeturl + '.json' ); 
0 1 212
1 REPLY 1

Here's what I use, it seems to do what you want.

<Javascript name='JS-AddJsonSuffix' timeLimit='200' >
  <Source>
    context.setVariable('target.copy.pathsuffix', false);
    var originalBaseTargetUrl = context.getVariable('target.url');
    var proxyPathSuffix = context.getVariable('proxy.pathsuffix');
    context.setVariable('target.url', originalBaseTargetUrl + proxyPathSuffix + '.json' );
  </Source>
</Javascript>

NB: the above uses a new feature in the JS policy in which you can specify the JS source for the policy right in the policy XML. No need for an external resource file.

I attach that policy in the preflow of the Target Endpoint, not the PostFlow as you did.

And here's an example request, passed-through to an "echo" service that shows the URL received at the backend:

$ curl -i https://$ORG-$ENV.apigee.net/addjson/t1/siuy/1
HTTP/1.1 200 OK
Date: Tue, 12 Feb 2019 17:34:01 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 499
Connection: keep-alive


{
  "url": "/t1/siuy/1.json",
  "method": "GET",
  "headers": {
    "host": "echo.herokuapp.com",
    "connection": "close",
    "x-forwarded-for": "50.46.221.133, 104.198.35.174",
    "x-forwarded-port": "443",
    "x-forwarded-proto": "https",
    "user-agent": "curl/7.61.1",
    "accept": "*/*",
    "x-request-id": "c38d561a-d630-4cf7-8cd3-1d39e779211d",
    "via": "1.1 vegur",
    "connect-time": "1",
    "x-request-start": "1549992841451",
    "total-route-time": "0"
  },
  "body": {}
}