Skip to content

Commit ae68665

Browse files
committed
add content-type requirement
1 parent 027e6ea commit ae68665

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ public async Task Invoke(HttpContext context)
2828
{
2929
_logger.LogInformation("Passing request to JsonApiService: " + context.Request.Path);
3030

31-
var wasHandled = _jsonApiService.HandleJsonApiRoute(context, _serviceProvider);
31+
if(context.Request.ContentType == "application/vnd.api+json") {
32+
var wasHandled = _jsonApiService.HandleJsonApiRoute(context, _serviceProvider);
33+
}
34+
else
35+
{
36+
_logger.LogInformation("Content-Type invalid for JsonAPI");
3237

33-
if(!wasHandled) {
34-
_logger.LogInformation("Request not handled by JsonApiService. Middleware pipeline continued.");
3538
await _next.Invoke(context);
39+
40+
RespondUnsupportedMediaType(context);
3641
}
37-
else {
38-
_logger.LogInformation("Request handled by JsonApiService. Middleware pipeline terminated.");
39-
}
42+
}
43+
44+
private void RespondUnsupportedMediaType(HttpContext context)
45+
{
46+
context.Response.StatusCode = 415;
47+
context.Response.Body.Flush();
4048
}
4149
}
4250
}

0 commit comments

Comments
 (0)