Discover Refinitiv
MyRefinitiv Refinitiv Perspectives Careers
Created with Sketch.
All APIs Questions & Answers  Register |  Login
Ask a question
  • Questions
  • Tags
  • Badges
  • Unanswered
Search:
  • Home /
  • Open PermID /
avatar image
REFINITIV
Question by tristan.hale · Nov 24, 2016 at 03:23 AM · permidjavascriptcalaisjs

Javascript (JS) code sample example for PermID Open Calais, please.

Hello

I can make an entity search request just fine by navigating to this...

https://api.thomsonreuters.com/permid/search?q=TRI∾cess-token=MY_ACCESS_TOKEN

...which displays some nice JSON in my browser.

Question is, how to I make this request programmatically using client-side javascript?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            
            var accessToken = "MY_ACCESS_TOKEN";
            var myURL = "https://api.thomsonreuters.com/permid/search?q=TRI";
            $.ajax({
                type: 'GET',
                format: 'JSON',  
                dataType: 'JSON',
                url: myURL,
                headers: {
                    'access-token': accessToken
                },
                success: function(data, status) {
                    console.log(data);
                }
            });

Predictably enough this returns the standard 'Access-Control-Allow-Origin' message:

XMLHttpRequest cannot load https://api.thomsonreuters.com/permid/search?q=TRI. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://developer.permid.org' that is not equal to the supplied origin. Origin 'http://localhost:9999' is therefore not allowed access.

What's the solution, please? Is it necessary to use a JSONP request perhaps, or is there some syntactical problem with my JSON AJAX request, e.g. the header is not specified correctly?

On the portal there are various code samples including JAVA but I don't see one for JS.

People who like this

0 Show 0
Comment
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

3 Replies

  • Sort: 
avatar image
REFINITIV
Best Answer
Answer by jirapongse.phuriphanvichai · Nov 28, 2016 at 02:20 AM

Please refer to the similar question in https://community.developers.thomsonreuters.com/questions/5752/cross-origin-request-not-possible.html.

Access-Control-Allow-Origin is a protection in the web browsers.

However, JavaScript works fine if running outside the web browsers, such as Node.js.

var https = require('https');

https.get({
    host: 'api.thomsonreuters.com',
    path: 'permid/search?q=TRI',
    headers: {'x-ag-access-token': 'ACCESS_TOEKEN'}
}, function (response) {
    // Continuously update stream with data
    var body = '';
    response.on('data', function (d) {
        body += d;
    });
    response.on('end', function () {        
        // Data reception is done, do whatever with it!
        var parsed = JSON.parse(body);
        console.log(parsed);
    });
});

It returns the following object.

{ result:
   { organizations:
      { entityType: 'organizations',
        total: 1704,
        start: 1,
        num: 5,
        entities: [Object] },
     instruments:
      { entityType: 'instruments',
        total: 77,
        start: 1,
        num: 5,
        entities: [Object] },
     quotes:
      { entityType: 'quotes',
        total: 403,
        start: 1,
        num: 5,
        entities: [Object] } } }

To use it on the web browser, you need to disable this protection in the web browser. Chrome has an extension called Allow-Control-Allow-Origin: * which allows Chrome to request any site with ajax from any source.

Comment
tristan.hale

People who like this

1 Show 0 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avatar image
REFINITIV
Answer by tristan.hale · Nov 28, 2016 at 02:47 AM

Thanks @jirapongse.phuriphanvichai

Your node.js script works a treat, so on the basis that the PermID API supports neither JSONP nor CORS, creating a simple wrapper which does is an effective solution.

(o:

Comment

People who like this

0 Show 0 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avatar image
REFINITIV
Answer by tristan.hale · Nov 28, 2016 at 03:27 AM

permid_server.js

var express = require('express');
var app = express();
var cors = require('cors');
var https = require('https');
var finalRes;

// Avoids LEAF error (may not be required)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

// Allow other domains to make CORS requests (if necessary)
app.use(cors());

// Example usage: http://localhost:8082/search/apple

// Initiate server and send confirmation of host/port to console
var server = app.listen(8082, function () {
   var host = server.address().address;
   var port = server.address().port;
   console.log("Example app listening at http://%s:%s", host, port);
});

// Service end-point (search)
app.get('/search/:searchString', function (req, res) {
    finalRes = res;
    var searchString = req["params"]["searchString"]; 
    console.log("Search request for: " + searchString);
    var accessToken = 'YOUR_TOKEN';
    var options = {
        host: 'api.thomsonreuters.com',
        path: 'permid/search?q=' + searchString,
        method: 'GET',
        headers: {'x-ag-access-token': accessToken,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
        }
    };
    var request = https.request(options, function(res) {
        res.setEncoding('utf8');
        var body = '';
        res.on('data', (chunk) => {
            body += chunk;
        });
        res.on('end', () => {
            dataReturned(body);        
        });
    });
    request.on('error', (e) => {
        console.log("Error: "+ e);
    });    
    request.end();    
});
function dataReturned(e) {
    console.log("Success!");
    finalRes.end(e);
}
Comment

People who like this

0 Show 0 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Watch this question

Add to watch list
Add to your watch list to receive emailed updates for this question. Too many emails? Change your settings >
8 People are following this question.

Related Questions

NLP Engine for development

How to get URL updates?

PermId Organisation RSS Feed no longer returning any data

Trying to use permid file match and receive error Failed to resolve API Key variable request.header.X-AG-Access-Token

How to get all the info on the PermID page via the lookup API

  • Feedback
  • Copyright
  • Cookie Policy
  • Privacy Statement
  • Terms of Use
  • Careers
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Alpha
  • App Studio
  • Block Chain
  • Bot Platform
  • Calais
  • Connected Risk APIs
  • DSS
  • Data Fusion
  • Data Model Discovery
  • Datastream
  • Eikon COM
  • Eikon Data APIs
  • Elektron
    • EMA
    • ETA
    • WebSocket API
  • Legal One
  • Messenger Bot
  • Messenger Side by Side
  • ONESOURCE
    • Indirect Tax
  • Open PermID
    • Entity Search
  • Org ID
  • PAM
    • PAM - Logging
  • ProView
  • ProView Internal
  • Product Insight
  • Project Tracking
  • Refinitiv Data Platform
    • Refinitiv Data Platform Libraries
  • Rose's Space
  • Screening
    • Qual-ID API
    • Screening Deployed
    • Screening Online
    • World-Check One
    • World-Check One Zero Footprint
  • Side by Side Integration API
  • TR Knowledge Graph
  • TREP APIs
    • CAT
    • DACS Station
    • Open DACS
    • RFA
    • UPA
  • TREP Infrastructure
  • TRIT
  • TRKD
  • TRTH
  • Thomson One Smart
  • Transactions
    • REDI API
  • Velocity Analytics
  • Wealth Management Web Services
  • World-Check Data File
  • Explore
  • Tags
  • Questions
  • Badges