Modify network calls

This is for:

Developer

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’re 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’re creating your own endpoint, you would need to use the following example.

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

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.

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)