Problem+json support (RFC7807)

I want to return error responses in the RFC7807 format. It’s just a json response with standardized properties. Setting it all up has been no problem, but getting ServiceStack to return the json to the client has not been successful.

My code:

CustomErrorHttpHandlers[HttpStatusCode.NotFound] = new CustomResponseHandler((httpReq, httpRes) =>
{
    httpReq.ResponseContentType = RFC7807ProblemDetails.PROBLEM_JSON_MIME_TYPE; //application/problem+json
    return new HttpError(/*dto creation omitted for brevity/*, HttpStatusCode.NotFound, "Not Found", null) { ContentType = RFC7807ProblemDetails.PROBLEM_JSON_MIME_TYPE };
});

In that example I have tried 2 ways to set the response content type, as you can see. However, that causes a crash in both cases:

ContentType not supported 'application/problem+json'

Any tips on getting around this block? This might be worth adding formal support for.

The error is a result of asking ServiceStack to return the response in a custom format that’s not been registered.

You should be able to set the content type and return the serialized response, e.g:

CustomErrorHttpHandlers[HttpStatusCode.NotFound] = 
    new CustomResponseHandler((httpReq, httpRes) =>
    {
        httpRes.ContentType = "application/problem+json";
        return "/*dto creation*/";
    });

ServiceStack has its own structured error responses which every client library & UI has built-in support for, we’re not going to add redundant support for an incompatible structured error response format.

On a side-note mandating keys with dashes (e.g. invalid-params) is a sure way to ensure friction and undue burden in anyone trying to implement or consume this format as it’s not possible to serialize into a typed model using a generic serializer without custom configuration to handle the non-standard variable names.

1 Like