Skip to content

Commit

Permalink
AspNetCore: Improve HTTP Status code on Submit failed and wrong Conte…
Browse files Browse the repository at this point in the history
…ntType (#526)

* Use StatusCodes.Status415UnsupportedMediaType for wrong content type
* Use 422 and 409 errors for submit failures
  • Loading branch information
Daniel-Svensson authored Nov 18, 2024
1 parent 612b00b commit 7eaf1f2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## AspNetCore - 1.3.1

* **New Http status codes** returned
* Submit can now return 409 and 422 Unprocessable Entity (for conflicts and validation failure)
* Use 415 unsupported media type instead of 400 for unsupported content type

# 5.6.0 / AspNetCore 1.3.0

## AspNetCore 1.3.0 (#518)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async Task Invoke(HttpContext context)
{
if (context.Request.ContentType != "application/msbin1")
{
context.Response.StatusCode = 400; // maybe 406 / System.Net.HttpStatusCode.NotAcceptable
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
return;
}
(_, inputs) = await ReadParametersFromBodyAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ protected Task WriteResponse(HttpContext context, object result)

var response = context.Response;
response.Headers.ContentType = "application/msbin1";
response.StatusCode = 200;
response.ContentLength = bufferMemory.Length;
response.Headers.CacheControl = "private, no-store";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override async Task Invoke(HttpContext context)
{
if (context.Request.ContentType != "application/msbin1")
{
context.Response.StatusCode = 400; // maybe 406 / System.Net.HttpStatusCode.NotAcceptable
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override async Task Invoke(HttpContext context)

if (context.Request.ContentType != "application/msbin1")
{
context.Response.StatusCode = 400; // maybe 406 / System.Net.HttpStatusCode.NotAcceptable
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
return;
}

Expand All @@ -50,6 +50,20 @@ public override async Task Invoke(HttpContext context)
return;
}

// Set HTTP StatusCode on failed requests
foreach (var change in result)
{
if (change.HasError)
{
if (change.HasConflict)
context.Response.StatusCode = StatusCodes.Status409Conflict;
else
context.Response.StatusCode = StatusCodes.Status422UnprocessableEntity;

break;
}
}

await WriteResponse(context, result);
}
catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested)
Expand Down

0 comments on commit 7eaf1f2

Please sign in to comment.