Prevent unwanted headers when returning 304 No change with ServiceStack

Using ServiceStack, I just want to return 304 Not Modified as such:

HTTP/1.1 304 Not Modified 

But ServiceStack adds many other unwanted (returning HttpResult with 304 code) headers per se:

 HTTP/1.1 304 Not Modified Content-Length: 0 Content-Type: application/json Server: Microsoft-HTTPAPI/2.0 X-Powered-By: ServiceStack/3.94 Win32NT/.NET Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: Content-Type Date: Tue, 07 Aug 2012 13:39:19 GMT 

How can I prevent the output of other headers? I tried various approaches with HttpResult , registering a filter like fictitious content, but since its name implies only control over the content, not the headers, or others listed here . I also tried to implement my own derived IHttpResult using IStreamWriter and IHasOptions with the same results: ServiceStack adds unwanted headers.

thanks

Update

I managed to remove the content-type using the following, but some headers are still present, i.e. content-length , server and date .

  public override object OnGet(FaultTypes request) { var result = new HttpResult { StatusCode = HttpStatusCode.NotModified, StatusDescription = "Not Modified", // Otherwise NotModified written! }; // The following are hacks to remove as much HTTP headers as possible result.ResponseFilter = new NotModifiedContentTypeWriter(); // Removes the content-type header base.Request.ResponseContentType = string.Empty; return result; } class NotModifiedContentTypeWriter : ServiceStack.ServiceHost.IContentTypeWriter { ServiceStack.ServiceHost.ResponseSerializerDelegate ServiceStack.ServiceHost.IContentTypeWriter.GetResponseSerializer(string contentType) { return ResponseSerializerDelegate; } void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToResponse(ServiceStack.ServiceHost.IRequestContext requestContext, object response, ServiceStack.ServiceHost.IHttpResponse httpRes) { } void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToStream(ServiceStack.ServiceHost.IRequestContext requestContext, object response, System.IO.Stream toStream) { } string ServiceStack.ServiceHost.IContentTypeWriter.SerializeToString(ServiceStack.ServiceHost.IRequestContext requestContext, object response) { return string.Empty; } public void ResponseSerializerDelegate(ServiceStack.ServiceHost.IRequestContext requestContext, object dto, ServiceStack.ServiceHost.IHttpResponse httpRes) { } } 
+6
source share
2 answers

The only headers emitted by ServiceStack are those registered with EndpointHostConfig.GlobalResponseHeaders .

Remove them if you do not want them to be emitted, for example:

 SetConfig(new EndpointHostConfig { GlobalResponseHeaders = new Dictionary<string,string>() }); 

You can add them adhoc based using HttpResult, for example:

 return new HttpResult(dto) { Headers = { { "Access-Control-Allow-Origin", "*" }, { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" } { "Access-Control-Allow-Headers", "Content-Type" }, } }; 

Both options are explained in more detail: REST API and CORS

+8
source

In fact, you can just do something similar in your API

 base.Response.StatusCode = (int) HttpStatusCode.NotModified; base.Response.EndHttpRequestWithNoContent(); return new HttpResult(); 

That would not return ContentType, ContentLength, etc.

0
source

Source: https://habr.com/ru/post/922286/


All Articles