Modifying Network Calls

Coveo JavaScript Search Framework 1.1550.5 (October 2016)

In some very specific setups, it might be necessary to have finer control on all network calls before they are sent by the browser.

For example, you might want to add or remove a special request header to pass to your network infrastructure.

The Coveo JavaScript Search Framework offers a way to configure the search endpoint with a callback function to modify the IRequestInfo object.

When Creating a New Search Endpoint

In a scenario where you are creating your own endpoint, you would need to use the following example.

Remember to replace my-auth-token with your authorization token.

When using Coveo Cloud V1, the restUri should be changed to https://cloudplatform.coveo.com/rest/search.

Coveo.SearchEndpoint.endpoints['default'] = new Coveo.SearchEndpoint({
    restUri : 'https://platform.cloud.coveo.com/rest/search',
    accessToken : 'my-auth-token',
    requestModifier : function(request) {
        // modify the method
        request.method = 'POST';
        // delete a header
        delete request.headers.Authorization;
        // add a new header
        request.headers.Foo = 'bar';
 
        // modify the URL
        request.url = 'somewhere.else.com';
 
        return request;
    }
})

When Modifying an Existing Search Endpoint

In some scenarios, such as when using Coveo for Salesforce, your search endpoint might already have been created for you. In this case, you would need to modify it using the following method.

Remember to replace my-auth-token with your authorization token.

When using Coveo Cloud V1, the restUri should be changed to https://cloudplatform.coveo.com/rest/search.

Coveo.SearchEndpoint.endpoints['default'] = new Coveo.SearchEndpoint({
    restUri : 'https://platform.cloud.coveo.com/rest/search',
    accessToken : 'my-auth-token'
});
 
var myRequestModifier = function(request) {
    // modify the method
    request.method = 'POST';
    // delete a header
    delete request.headers.Authorization;
    // add a new header
    request.headers.Foo = 'bar';
 
    // modify the URL
    request.url = 'somewhere.else.com';
    return request;
}
 
// The setRequestModifier function exists on all SearchEndpoint ( https://coveo.github.io/search-ui/classes/searchendpoint.html )
Coveo.SearchEndpoint.endpoints['default'].setRequestModifier(myRequestModifier)