Search code examples
azure-cognitive-search

Setting and retrieving custom HTTP Headers


We are testing some preview features on Azure Search with the 1.1.1 SDK and need to send and receive custom HTTP headers on our searches.

Based on the Migration Guide we found that there is an underlying AzureOperationResponse that can be used to access the headers.

Right now we are doing our searches with:

SearchResults result = await client.Documents.SearchAsync(searchText, parameters);

How can we send/receive custom headers with this SDK version?


Solution

  • The easiest way to achieve this with the current SDK is to use the SearchWithHttpMessagesAsync method.

    By taking the current code, we can change it to:

    var customHeaders = new Dictionary<string, List<string>>() { { "header1", new List<string>() { "value1" } }, { "header2", new List<string>() { "value2" } } };
    var response = await client.Documents.SearchWithHttpMessagesAsync(searchText, parameters, null, customHeaders);
    var headerValue1 = response.Response.Headers.GetValues("header1").Aggregate((x, y) => x + y);
    var headerValue2 = response.Response.Headers.GetValues("header2").Aggregate((x, y) => x + y);
    SearchResults results = response.Body;
    

    This way we can send any custom header and receive any custom header.