question

Upvotes
Accepted
76 15 21 29

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.

intelligent-tagging-apipermid-apiopen-permid-apiintelligent-taggingjavascript
icon clock
10 |1500

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

Upvote
Accepted
38.1k 71 35 53

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.

icon clock
10 |1500

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

Upvotes
76 15 21 29

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:

icon clock
10 |1500

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

Upvotes
76 15 21 29

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);
}
icon clock
10 |1500

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

Click below to post an Idea Post Idea