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

Content-Type for BadRequest with ProblemDetails-object #5

Closed
nengberg opened this issue Sep 14, 2018 · 4 comments
Closed

Content-Type for BadRequest with ProblemDetails-object #5

nengberg opened this issue Sep 14, 2018 · 4 comments

Comments

@nengberg
Copy link

nengberg commented Sep 14, 2018

Hi and thanks for your middlware for ProblemDetails! 👍

This is more a question and not an issue (?):

When you return BadRequest with an object that is or derives from ProblemDetails the Content-Type will be application/json.

var problem = new OutOfCreditProblemDetails
{
     ...
};

return BadRequest(problem);

But when you return BadRequest without any object the Content-Type will be application/problem+json.

return BadRequest();

Is that by design?

Regards
Niklas

@khellang
Copy link
Owner

Is that by design?

Not really. In the former case, it's actually MVC writing the body. The middleware is never involved. While the latter is just setting the status code and the middleware takes care of writing the body, which is why it has the correct content type.

Unfortunately, there's not much I can do about this in this repo, but I can take a stab at sending a PR to MVC to detect ProblemDetails and set the correct content type during result execution later.

A workaround is to specify the content type directly when returning the result from the action.

@nengberg
Copy link
Author

That would be great! Thanks for the quick response!

@khellang
Copy link
Owner

I've filed aspnet/Mvc#8467. I think that's the best place to track this.

If you need a workaround, you should explicitly add the content types:

var result = new ObjectResult(details)
{
StatusCode = details.Status ?? context.Response.StatusCode,
DeclaredType = details.GetType(),
};
result.ContentTypes.Add("application/problem+json");
result.ContentTypes.Add("application/problem+xml");

@khellang
Copy link
Owner

khellang commented Sep 28, 2018

Looks like this will be fixed in ASP.NET Core 2.2 Preview 3; aspnet/Mvc#8512

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

No branches or pull requests

2 participants