dherault / dherault/serverless-offline

CONVERT_TO_TEXT parameter does not convert binary data of the request to base64

Open
#1,774 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
811
Avg merge
2d 4h
Merged PRs (30d)
3

Description

## Bug Report

This is the very exact bug as reported in https://github.com/dherault/serverless-offline/issues/1107, which was closed as resolved, but it doesn't look like it is.

A very simple configutation setting in the request that it converts the payload to base64 (`contentHandling: CONVERT_TO_TEXT`) is not converting the body to base64, eo `event.payload` is not the expected base64-encoded data.

**Current Behavior**

**Sample Code**

- file: serverless.yml

```yaml
service: my-service

plugins:
- serverless-offline

provider:
runtime: nodejs18.x
stage: dev

functions:
hello:
events:
- http:
method: post
path: hello
request:
contentHandling: CONVERT_TO_TEXT
handler: handler.hello
```

- file: handler.js

```js
"use strict"

const { stringify } = JSON

exports.hello = async function hello(event) {

// event.payload should be encoded as base64
return {
body: stringify({ foo: "bar" }),
statusCode: 200,
}
}
```

**Expected behavior/code**

event.payload should be encoded as base64 when contentHandling is set to CONVERT_TO_TEXT

**Environment**

- `serverless` version: v3.38.0
- `serverless-offline` version: v13.3.3
- `node.js` version: v18.3.0
- `OS`: Ubuntu 20.04

**Possible Solution**

I think the error is in the HttpServer https://github.com/dherault/serverless-offline/blob/c85a19272c59ad9e7cf1aea74e3442c7607b533a/src/events/alb/HttpServer.js#L176 where it always encodes the payload like this

```
// Payload processing
const encoding = detectEncoding(request)

request.payload = request.payload && request.payload.toString(encoding)
```
detectEncoding only returns either `utf8` or `binary`, but it should be `base64` for request contentHandling set to CONVERT_TO_TEXT

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.