# Using official nodejs elasticsearch npm package on AWS Lambda Docker container sets the Content-Type header to \[text/plain\]

**URL:** https://discuss.elastic.co/t/using-official-nodejs-elasticsearch-npm-package-on-aws-lambda-docker-container-sets-the-content-type-header-to-text-plain/348766
**Category:** Elasticsearch
**Tags:** docker, runtime-fields
**Created:** [December 6, 2023, 11:45pm UTC](https://discuss.elastic.co/t/using-official-nodejs-elasticsearch-npm-package-on-aws-lambda-docker-container-sets-the-content-type-header-to-text-plain/348766 "2023-12-06T23:45:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![asnyameeteen](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@asnyameeteen](https://discuss.elastic.co/u/asnyameeteen)
#### Post date: [December 6, 2023, 11:45pm UTC](https://discuss.elastic.co/t/using-official-nodejs-elasticsearch-npm-package-on-aws-lambda-docker-container-sets-the-content-type-header-to-text-plain/348766/1 "2023-12-06T23:45:32Z")

</div>

I am trying to publish a document to my self-hosted elasticsearch running on AWS. I wrote my code in nodejs, using v18 LTS, using @elastic/elasticsearch npm module version 8.10.0. When I run my code locally, outside of Docker, it works and my document is published. When I run it as a Lambda function from AWS, it fails with the following error:

```auto
ERROR	Promise was not fulfilled {
      status: 'rejected',
      reason: ResponseError: {"error":"Content-Type header [text/plain] is not supported","status":406}
          at SniffingTransport.request (/var/task/node_modules/@elastic/transport/lib/Transport.js:479:27)
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async Client.CreateApi [as create] (/var/task/node_modules/@elastic/elasticsearch/lib/api/api/create.js:43:12)
          at async Promise.allSettled (index 1)
          at async Runtime.processEvent [as handler] (/var/task/index.js:105:27) {
        meta: {
          body: [Object],
          statusCode: 406,
          headers: [Object],
          meta: [Object],
          warnings: [Getter]
        }
      }
    }

```

Once I received this error I modified my client creation to the following:

```auto
elasticsearch: {
        //node: process.env.ELASTICSEARCH_HOST,
        node: {
            url: new URL(process.env.ELASTICSEARCH_HOST),
            headers: {
                'content-type': 'application/vnd.elasticsearch+json; compatible-with=8',
            },
        },
        auth: {
            username: process.env.ELASTICSEARCH_USERNAME,
            password: process.env.ELASTICSEARCH_PASSWORD,
        },
        tls: {
            ca: fs.readFileSync(process.env.CA_FILE),
            rejectUnauthorized: true
        }
    }

```

and I call `const esClient = new Client(elasticsearch);` to create the client. I still get the same error when I try this. My Dockerfile is simple

```auto
FROM amazon/aws-lambda-nodejs:18
WORKDIR $LAMBDA_TASK_ROOT
# Copy only the files for running in Lambda
COPY package*.json .
RUN npm ci --omit=dev
COPY . .

CMD ["index.processEvent"]

```

I cannot figure out why the Content-Type header is being set to [text/plain]

---

<div class="post-metadata">

### Author: ![asnyameeteen](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@asnyameeteen](https://discuss.elastic.co/u/asnyameeteen)
#### Post date: [December 8, 2023, 11:12pm UTC](https://discuss.elastic.co/t/using-official-nodejs-elasticsearch-npm-package-on-aws-lambda-docker-container-sets-the-content-type-header-to-text-plain/348766/2 "2023-12-08T23:12:43Z")

</div>

I ran the Lambda locally on a Mac and it was successful. So, running locally with the same Dockerfile works and running on AWS Lambda does not.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 5, 2024, 11:12pm UTC](https://discuss.elastic.co/t/using-official-nodejs-elasticsearch-npm-package-on-aws-lambda-docker-container-sets-the-content-type-header-to-text-plain/348766/3 "2024-01-05T23:12:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
