Search code examples
javascripthttpgetclickhouse

JavaScript - ERR_EMPTY_RESPONSE


I am new to programming. Please help me solve the problem, my work has stopped. I have a ClickHouse database. I need to perform a GET request on it and get tabular data. The post is completed successfully via Postman. But as soon as I try to execute it from JavaScript via Google Chrome I get the error "ERR_EMPTY_RESPONSE". I looked at a bunch of sites and realized that the problem was CORS. All servers are located on the corporate work network and, if I'm not mistaken, CORS is not particularly important in this case. How can I fix my code so that I can avoid CORS restrictions and continue my work. The POST method is prohibited on the ClickHouse server. At best, I wouldn’t want to touch the server itself with the database at all. If anyone has encountered this, please help!

async function getDataFromClickHouse ()
{

const _baseUrl = 'http://servername:8123/';
const _username = 'login';
const _password = 'password';
const _query = 'SELECT * FROM any_table';

const base64Credentials = btoa(`${_username}:${_password}`);
const url = `${_baseUrl}/?query=${encodeURIComponent(_query)}`;

try {
    const response = await fetch(url, {
        method: 'GET',
        headers: {
            'Content-Type' : 'application/json',
            'Authorization': `Basic ${base64Credentials}`,
            'Access-Control-Allow-Origins' : '*'
        }
    });
    if (response.ok) {
        return await response.json();
    } else {
        throw new Error(`Error executing ClickHouse query: ${response.statusText}`);
    }
}
catch (error) {
    console.error(error);
}
}

When I set mode : 'no-cors' I get 403 Forbidden (Authentication failed) from the server. Despite the fact that I double-checked the authorization data via Postman

Thanks in advance for any advice and recommendations. This is my first post here, do not judge harshly for the perhaps incorrect formulation of the question


Solution

  • if you try to access to your clickhouse-server direcly from browser (this is not recommended way)

    then add to /etc/clickhouse-server/config.d/options_method.xml

    <clickhouse>
        <http_options_response>
            <header>
                <name>Access-Control-Allow-Origin</name>
                <value>*</value>
            </header>
            <header>
                <name>Access-Control-Allow-Headers</name>
                <value>origin, x-requested-with, content-type, authorization, content-type, x-clickhouse-user, x-clickhouse-key</value>
            </header>
            <header>
                <name>Access-Control-Allow-Methods</name>
                <value>POST, GET, OPTIONS</value>
            </header>
            <header>
                <name>Access-Control-Allow-Credentials</name>
                <value>true</value>
            </header>
            <header>
                <name>Access-Control-Max-Age</name>
                <value>86400</value>
            </header>
    
        </http_options_response>
    </clickhouse>