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&access-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.
Best Answer
-
Please refer to the similar question in https://community.developers.refinitiv.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.
1
Answers
-
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:
0 -
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);
}0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛