Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

http-json-body-parser - json a default content type #1120

Closed
wants to merge 1 commit into from

Conversation

qoomon
Copy link
Contributor

@qoomon qoomon commented Oct 30, 2023

No description provided.

@willfarrell
Copy link
Member

Thanks for putting a PR together. Can you share your use case that requires this change. Looks like you want a way to allow invalid http request for some reason.

@qoomon
Copy link
Contributor Author

qoomon commented Oct 30, 2023

I'd like the server/lambda to assume content-type is application/json if content-type header is not set.

@willfarrell
Copy link
Member

I would suggest having the request send the proper headers. If that's not possible a custom normalizer middleware to clean up the invalid request can be added. Something like:

.before((request) => {
  if (!request.event.headers.['content-type'] && request.event.body.substring(0,1) === '{') {
    request.event.headers.['content-type'] = 'application/json'
  }
})

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html

Reflecting on this a little more, I wonder if the http-event-normalizer should have an option to set a default Content-Type for those that don't want their endpoint to be as strict.

@qoomon
Copy link
Contributor Author

qoomon commented Oct 31, 2023

On one site I understand your points, on the other side it still feel a little odd because the opposite middleware httpJsonBodySerializer has a default defaultContentType option.

@willfarrell
Copy link
Member

http-json-body-parser interprets headers from other developers (out of your control). http-response-serializer interprets headers from the developer that made the response (in your control).

@qoomon
Copy link
Contributor Author

qoomon commented Nov 3, 2023

Because of http-json-body-parser interprets headers out of my control I want at least control how to interpret a content if no content type header is set.

@willfarrell
Copy link
Member

I just pushed a feature into http-header-normalizer to allow default headers. It will be in 4.7.0

@willfarrell willfarrell closed this Nov 8, 2023
@qoomon qoomon deleted the patch-2 branch November 10, 2023 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants