Services returning straight HttpResult to typescript client

I am getting some strange behavior with the typescript client as follows:

My Service does this:

return new HttpResult() { StatusCode = System.Net.HttpStatusCode.OK };

With a method return type of object and the DTO interface is IReturnVoid (I’ve tried different options there…)

Typescript client gets this:
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected end of JSON input
SyntaxError: Unexpected end of JSON input
at JsonServiceClient.push…/node_modules/@servicestack/client/dist/index.js.JsonServiceClient.createResponse (index.js:748)

Which when looking at the source it looks like the service is setting headers as if its returning JSON which it is not and that is confusing the client

Looking for advice on how to properly implement server and client side when all you return is a status code.

Thanks.

200 OK is the default so you shouldn’t need to return anything. If your Service is IReturnVoid your method signature should be void or your Service should return null.

Although if your Service doesn’t have a response ServiceStack will return a 204 No Content which is preferred, but you can leave it as a 200 OK Response by overriding Config.Return204NoContentForEmptyResponse = false.

Customize HTTP Responses shows different ways to customize the HTTP Response.

E.g. you can set the StatusCode with:

base.Response.StatusCode = (int)HttpStatusCode.OK;

But this has no effect with OK responses which is the default.

So is then the right approach if you want a 404 from an IreturnVoid you would do:

base.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;

?

Typically you’d throw an Exception:

throw HttpError.NotFound("Resource does not exist");

If you’re going to just set the status code, it’s a good idea to terminate the request immediately:

base.Response.StatusCode = (int)HttpStatusCode.NotFound;
base.Response.EndRequest(); //Short-circuits Request Pipeline
1 Like