Skip to content

custom_headers are not properly propagated to Index instances. #1143

@eedeebee

Description

@eedeebee

Below is a workaround

class FixedMeilisearchClient(meilisearch.Client):
    """
    Fixed Meilisearch client that properly propagates custom_headers to Index instances.

    This fixes a bug in the official client where custom_headers provided to the Client
    are not passed to Index instances, causing authentication failures.
    """

    def __init__(
        self, url, api_key=None, timeout=None, client_agents=None, custom_headers=None
    ):
        """
        Initialize the fixed Meilisearch client.

        Args:
            url: Meilisearch server URL
            api_key: API key for authentication (can be empty when using custom_headers)
            timeout: Request timeout in seconds
            client_agents: Additional client agent information
            custom_headers: Custom headers (e.g., for bearer token authentication)
        """
        super().__init__(url, api_key, timeout, client_agents, custom_headers)
        self._custom_headers = custom_headers

    def index(self, uid):
        """
        Get an index with properly configured custom headers.

        Args:
            uid: Index UID

        Returns:
            Index instance with fixed HTTP client
        """
        index = super().index(uid)
        # Fix the index's http instance to include custom headers
        if self._custom_headers:
            index.http = HttpRequests(index.config, self._custom_headers)
        return index

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions