elastic / elasticsearch-js

Official Elasticsearch client library for Node.js
https://ela.st/js-client
Apache License 2.0
29 stars 731 forks source link

Client sent an HTTP request to an HTTPS server #2497

Open ayelsew opened 4 days ago

ayelsew commented 4 days ago

🐛 Bug report

When I try to make a search request, server responds with "Client sent an HTTP request to an HTTPS server", even if I use a HTTPS URL

I put an console.log inside the transport layer of the library, and I saw this:

// /node_modules/.deno/@elastic+transport@8.9.1/node_modules/@elastic/transport/lib/Transport.js:544:27
{
  body: "Client sent an HTTP request to an HTTPS server.\n",
  statusCode: 400,
  headers: {},
  meta: {
    context: null,
    request: {
      params: {
        method: "POST",
        path: "/_search",
        body: '{"_source":false,"query":{"term":{"_id":"xpto"}}}',
        querystring: "",
        headers: {
          "user-agent": "elasticsearch-js/8.15.2 (linux 6.11.9-zen1-1-zen-x64; Node.js 20.11.1; Transport 8.9.1)",
          "x-elastic-client-meta": "es=8.15.2,js=20.11.1,t=8.9.1,hc=20.11.1",
          authorization: "ApiKey omited",
          "content-type": "application/vnd.elasticsearch+json; compatible-with=8",
          accept: "application/vnd.elasticsearch+json; compatible-with=8",
          "content-length": "49"
        }
      },
      options: {},
      id: 1
    },
    name: "elasticsearch-js",
    connection: {
      url: "https://XPTO.aws.found.io/",
      id: "https://XPTO.aws.found.io/",
      headers: {
        "user-agent": "elasticsearch-js/8.15.2 (linux 6.11.9-zen1-1-zen-x64; Node.js 20.11.1; Transport 8.9.1)",
        "x-elastic-client-meta": "es=8.15.2,js=20.11.1,t=8.9.1,hc=20.11.1"
      },
      status: "alive"
    },
    attempts: 0,
    aborted: false
  },
  warnings: [Getter]
}

It are using the https:// schema, so think "I did the right thing"

Also I tested the last version 8.16.2, and had the same behavior

FYI: I had performed the same request using cURL or Postman, and it worked.

I tried to use the cloud ID too but it throw me the follow error:

const client = new Client({
    cloud: { id: "xpto" }
    auth: {
      apiKey: "XPTO"
    }
});
error: Uncaught (in promise) TypeError: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at _from (ext:deno_node/internal/buffer.mjs:217:9)
    at Function.from (ext:deno_node/internal/buffer.mjs:229:10)
    at new Client (file:///home/ayelsew/Desktop/workers/node_modules/.deno/@elastic+elasticsearch@8.15.2/node_modules/@elastic/elasticsearch/lib/client.js:91:38)
    at file:///home/ayelsew/Desktop/workers/src/infra/elastic/elastic_client.ts:3:16

But bout the cloud ID, I guess it is a Deno's limitation not elastic client.

To reproduce

setup client:

import { Client } from "@elastic/elasticsearch";

const client = new Client({
    node: "https://XPTO.aws.found.io:443",
    auth: {
      apiKey: "XPTO"
    }
});

Do a request search

client.search({
  _source: false,
  query: {
    term: { _id: 'xpto' },
  },
});

Expected behavior

I expect that search() method returns the result instead of throwing error "Client sent an HTTP request to an HTTPS server"

Node.js version

20.11.1

@elastic/elasticsearch version

8.15.2

Operating system

Garuda linux 6.11.9-zen1-1-zen-x64

Any other relevant environment information

deno 2.1.1 Elastic search version v8.15.2