Skip to content

add content-type interceptor#226

Open
cfaur09 wants to merge 3 commits intomainfrom
SERVICES-2496-add-support-for-application-json-content-type-for-post-requests
Open

add content-type interceptor#226
cfaur09 wants to merge 3 commits intomainfrom
SERVICES-2496-add-support-for-application-json-content-type-for-post-requests

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