Skip to content

Commit 9f70e2f

Browse files
committed
try parsing as json if no content-type headers are present
1 parent 698d865 commit 9f70e2f

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
66

7+
## [3.6.2]  (2020-06-21)
8+
9+
### Updated
10+
11+
- Try harder to parse api payloads as JSON even if no content-type headers are available
12+
713
## [3.6.1]  (2020-06-21)
814

915
### Updated

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@manwaring/lambda-wrapper",
33
"description": "A lambda handler wrapper to abstract common functionality and provide useful defaults",
4-
"version": "3.6.1",
4+
"version": "3.6.2",
55
"scripts": {
66
"publish-please-dry-run": "publish-please --dry-run",
77
"publish-please": "publish-please",

src/api/parser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ describe('Body parsing', () => {
3131
expect(body).toEqual(form);
3232
});
3333

34-
it("Passes body through when content type isn't specified", () => {
34+
it("Tries to parse body as JSON when content type isn't specified", () => {
3535
const json = { hello: 'world' };
3636
const headers = {};
3737
const body = new Body(JSON.stringify(json), headers).getParsedBody();
38-
expect(body).toEqual(JSON.stringify(json));
38+
expect(body).toEqual(json);
3939
});
4040

4141
it("Errors when encoding and content-type don't match", () => {

src/api/parser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ export class Body {
4242
} else if (this.isJSON(contentType)) {
4343
parsedBody = JSON.parse(this.body);
4444
} else {
45-
logger.error('Content-Type header not found, unable to parse body');
46-
parsedBody = this.body;
45+
logger.error('Content-Type header not found, attempting to parse as JSON');
46+
parsedBody = JSON.parse(this.body);
4747
}
4848
} catch (err) {
49-
logger.error('Error parsing body', err, this.body);
49+
logger.error('Error parsing body, returning as-is', err, this.body);
5050
parsedBody = this.body;
5151
}
5252
}
5353
return parsedBody;
5454
}
5555

5656
private getContentType(): string {
57-
return this.headers['Content-Type'] || this.headers['CONTENT-TYPE'] || this.headers['content-type'];
57+
return this?.headers['Content-Type'] || this?.headers['CONTENT-TYPE'] || this?.headers['content-type'];
5858
}
5959

60-
private isFormUrlEncoded(contentType: string): boolean {
61-
return contentType && contentType.toUpperCase().includes('APPLICATION/X-WWW-FORM-URLENCODED');
60+
private isFormUrlEncoded(contentType?: string): boolean {
61+
return contentType?.toUpperCase().includes('APPLICATION/X-WWW-FORM-URLENCODED');
6262
}
6363

6464
private isJSON(contentType: string): boolean {
65-
return contentType && contentType.toUpperCase().includes('APPLICATION/JSON');
65+
return contentType?.toUpperCase().includes('APPLICATION/JSON');
6666
}
6767
}

0 commit comments

Comments
 (0)