Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return only status code in POST request in servicestack Instead of HTML page

Tags:

servicestack

have created REST service using servicestack and in post request I have return object in following way

  return new HttpResult(request)
                {
                    StatusCode = HttpStatusCode.Created,

                };

request: object which i have posted in database

servicestack post request

When i check it in fiddler it render whole HTML Page of servicestack in response body, instead of that i would like to return Status code only, so please tell me how can i do?

Thanks

like image 819
Arun Rana Avatar asked Nov 16 '11 12:11

Arun Rana


1 Answers

There was a bug in versions before < v3.05 that did not respect the HttpResult ContentType in some scenarios, it should be fixed now with the latest version of ServiceStack on NuGet or available from:

https://github.com/ServiceStack/ServiceStack/downloads

Prior to this you can still force the desired ContentType by changing the Accept:application/json Request Header on HttpClient or by appending ?format=json on the querystring of your url.

So now if you don't want to have any DTO serialized, you don't add it to the HttpResult:

return new HttpResult() { StatusCode = HttpStatusCode.Created };

Note you still might get an empty Html response back if calling this service in the browser (or any Rest Client that Accepts:text/html). You can force a ContentType that won't output any response if it has empty payload (e.g JSON/JSV) by specifying it in the result as well, e.g;

return new HttpResult() { 
    StatusCode = HttpStatusCode.Created,
    ContentType = ContentType.Json
};
like image 102
mythz Avatar answered Oct 11 '22 12:10

mythz