如何确保ServiceStack始终返回JSON? [英] How to ensure that ServiceStack always returns JSON?
问题描述
我们已决定仅允许带有Content-Type标头"application/json"的请求. 因此,每当我们收到带有替代内容或缺少Content-Type标头的请求时,都会抛出HttpError.此应该返回400响应,其中包含带有相关信息的JSON ResponseStatus主体. 但是,如果发送了Content-Type文本/纯文本,则会抛出HttpError,但是响应的Content-Type是text/plain且content-length:0.我希望返回ServiceStack的ResponseStatus.如果我向请求添加Accept application/json标头,则ResponseStatus返回的状态很好. 我使用邮递员执行了请求. Fiddler4的屏幕截图:
We have decided to only allow requests with a Content-Type header "application/json". So, whenever we receive a request with an alternative or missing Content-Type header, we throw an HttpError. This should return a 400 response containing a JSON ResponseStatus body with relevant info. However, if a Content-Type text/plain is sent, we throw an HttpError, but the response's Content-Type is text/plain and content-length: 0. I expected ServiceStack's ResponseStatus to be returned. The ResponseStatus is returned fine if I add an Accept application/json header to the request. I executed the request using Postman. Fiddler4 screen shot:
我知道Postman添加了Accept /标头.所以我的问题是:无论请求的Accept报头如何,我如何确保抛出HttpError总是将ResponseStatus返回为JSON?
I am aware that Postman adds the Accept / header. So my question is: How can I ensure that a thrown HttpError always return the ResponseStatus as JSON, no matter the request's Accept header?
SetConfig:
The SetConfig:
SetConfig(new HostConfig
{
EnableFeatures = Feature.All.Remove( Feature.Html | Feature.Csv | Feature.Jsv | Feature.Xml | Feature.Markdown | Feature.Razor | Feature.Soap | Feature.Soap11 | Feature.Soap12 | Feature.PredefinedRoutes),
DebugMode = false,
DefaultContentType = MimeTypes.Json
});
据我了解,DefaultContentType仅在请求中没有Accept标头的情况下使用.
As I understand it, the DefaultContentType is only used whenever there isn't an Accept header in the request.
PreRequestFilter:
The PreRequestFilter:
PreRequestFilters.Add((request, response) =>
{
if (request.Verb.Equals("OPTIONS"))
response.EndRequest();
if (request.GetHeader("Content-Type") == null || !request.GetHeader("Content-Type").Equals(MimeTypes.Json))
throw new HttpError((int)HttpStatusCode.BadRequest, "Bad request", "Expected a Content-Type header with an application/json value but found none. See http://docsdomain.com/ for any required headers.");
});
推荐答案
HTTP Accept
标头是客户端用来指示应返回哪种响应类型的标头,但是您可以通过添加全局请求来覆盖它以始终返回JSON过滤并明确设置ResponseContentType
,例如:
The HTTP Accept
header is what the client uses to indicate what Response Type should be returned but you can override this to always return JSON by adding a Global Request Filter and explicitly setting the ResponseContentType
, e.g:
GlobalRequestFilters.Add((req,res,dto) =>
req.ResponseContentType = MimeTypes.Json);
如果Accept
标头未指定特定的响应类型,则默认使用PreferredContentTypes
,您可以通过以下方式进行更改:
If the Accept
Header doesn't specify a specific Response Type it will default to using the PreferredContentTypes
which you can change by:
SetConfig(new HostConfig {
PreferredContentTypes = new []{ MimeTypes.Json }.ToList(),
});
这篇关于如何确保ServiceStack始终返回JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!