{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
1
Question by sumiya · Nov 09, 2015 at 11:06 AM · 583 Views javascriptApigee APIextractscript variables

How to extract multiple values from the URI in java script.

http://example.com/mail?$filter=(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')

Given a request like the above, how can I get the values of Mail_ID. i.e; SS123456, 10021433, BB987652 ?

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

1 Answer

  • Sort: 
avatar image
0

Answer by Dino   · Nov 10, 2015 at 02:51 AM

You're asking about how to use Javascript callout within Apigee Edge to do some parsing of a search or query string contained in a URI. There's nothing special in the JavaScript that can be run within Apigee Edge - so you'd just use the regular string and array methods: split, substring, map... that are provided by Javascript.

There is one special part of Apigee Edge that you may not be aware of - and that is the ability of Javascript callouts in Apigee Edge to reference external JS files. For example, there's a nice URI parsing library called URI.js available at medialize.github.io . This can be used within a JS callout. It needs to be referenced like this:

<Javascript name='Javascript-ParseQparam' timeLimit='200' >
  <Properties>
    <!-- to retrieve properties in js code:   properties.prop1 -->
    <Property name='prop1'>value-here</Property>
  </Properties>
  <IncludeURL>jsc://URI.js</IncludeURL>
  <ResourceURL>jsc://parseQparam.js</ResourceURL>
</Javascript>

The parseQparam might look like this:

// parseQparam.js
// ------------------------------------------------------------------
//
// shows how to use URI to grab query params, and then parses them.
//
// created: Mon Nov  9 18:23:02 2015
// last saved: <2015-November-09 18:39:10>


var sampleUrl = "http://example.com/mail?$filter=(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')";


// URI is from http://medialize.github.io/URI.js, and is included separately via
// this line in the policy:
//   <IncludeURL>jsc://URI.js</IncludeURL>


var uri = URI(sampleUrl);


// get data map:
var search = uri.search(true);
var filter = search.$filter; // "(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')"
filter = filter.substring(1, filter.length - 1); // remove parens
var parts = filter.split(' or ');  // assumes clean, well-formed logical clauses, no nesting


var ids = parts.map(function(item){
      var pair = item.split(' eq '); // assumes each clause has an eq operation
      return pair[1];
    });


context.setVariable('ids',ids.join(',')); // 'SS123456','10021433','BB987652'

Of course, you'd want to design your JS to be more resilient in the face of formatting abnormalities, like ill-formed conditional statements, and so on.

Parsing a boolean logic statement such as you offer, reliably, is a little more complicated. You shouldn't try doing that with just Javascript split, substring and map functions. It's better to rely on something that is already built and tested. For example, you can use the JSEP library (https://github.com/soney/jsep ) for parsing boolean logic. This library also is usable within an Apigee Edge callout. Include it in the same way you would include the URI.js library I showed above.

Then use it like so:

// customize jsep
jsep.removeBinaryOp("+");
jsep.removeBinaryOp("-");

jsep.addBinaryOp("or", 10);
jsep.addBinaryOp("and", 10);
jsep.addBinaryOp("eq", 100);
jsep.addBinaryOp("ne", 100);

var valueToParse = "Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652'";
var parseTree = jsep(valueToParse);

The output would look like this:

{
  "type": "BinaryExpression",
  "operator": "or",
  "left": {
    "type": "BinaryExpression",
    "operator": "or",
    "left": {
      "type": "BinaryExpression",
      "operator": "eq",
      "left": {
        "type": "Identifier",
        "name": "Mail_ID"
      },
      "right": {
        "type": "Literal",
        "value": "SS123456",
        "raw": "'SS123456'"
      }
    },
    "right": {
      "type": "BinaryExpression",
      "operator": "eq",
      "left": {
        "type": "Identifier",
        "name": "Mail_ID"
      },
      "right": {
        "type": "Literal",
        "value": "10021433",
        "raw": "'10021433'"
      }
    }
  },
  "right": {
    "type": "BinaryExpression",
    "operator": "eq",
    "left": {
      "type": "Identifier",
      "name": "Mail_ID"
    },
    "right": {
      "type": "Literal",
      "value": "BB987652",
      "raw": "'BB987652'"
    }
  }
}

Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way I can convert seconds to milliseconds in an Extract Variable Policy? 1 Answer

How can I see the service callout response headers in javascript? 1 Answer

Javascript callout not passing SSL certificate intermittently 1 Answer

How to extract a SHA1 Certificate field using crypto object ? 2 Answers

specifying proxy endpoint 4 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2019 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Members
  • Badges