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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add content-type interceptor #226

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cfaur09
Copy link
Contributor

@cfaur09 cfaur09 commented Jul 10, 2024

For POST requests, check if content-type has value application/json
If not, return 400 Bad Request with a proper message

@bogdan-rosianu bogdan-rosianu self-requested a review July 10, 2024 12:24
import { Observable } from 'rxjs';

@Injectable()
export class ContentTypeInterceptor implements NestInterceptor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more appropriate name would be JsonContentTypeInterceptor since it handles only the JSON content type.

export class ContentTypeInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
if (request.method === 'POST' && request.headers['content-type'] !== 'application/json') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should also be applicable for PUT/PATCH requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants