Hi, In Xdata I send response in Tstream format from my services, for preserve entire content (Json already formatted). But Compress MIddleware don't compress response content.
Is this "by design"? I have to compress Tstream from myself?
Thanks
Compress middleware will only compress the response if some conditions are met:
- Client sends an
accept-encoding
header with valuegzip
ordeflate
- Response size is bigger than the threshold specified in the middleware (1024 bytes by default)
- Response content-type is "compressible" (most text types, like
text/html
,text/plain
,application/json
, etc.
My test example (with Compress MIddleware in XdataServer middleware list):
function TGenService.JSONRPC(JsonRPC:string; method:string; params:TJsonObject; id:string): Tstream;
var
User: IUserIdentity;
begin
User:=Authorization('','10');
if (JsonRPC <> '2.0') or (trim(method) = '') then //or (id <= 0)
raise EXDataHttpForbidden.Create('JsonRPC non conforme');
TXDataOperationContext.Current.Response.Headers.setValue('content-type', 'application/json');
result:=JSONRPCMethods(method,params,User);
end;
But the stream is not compressed in Postman (size is always the same). Any hint on this?
Thanks
Are all conditions met?
Yes, I think. In the body result there is a Json.
I thought the only reason could be the Tstream type result...
Do you have a screenshot of the Postman request headers?
Do you have a screenshot showing the size of the response?
Are you sure Postman is not automatically decompressing the response?
Hi Wagner,
after some time,
yes, the Tstream in response isn't automatically compress in service.
I have 'Accept-Encoding = gzip, deflate' automatically in request headers.
(If I use an entity rest request have 'Accept-Encoding = gzip' without deflate).
Also in the browser the size received is the same, not compressed
I'm afraid I need a project and/or steps to reproduce the issue. Can you please send it?
Hi,
It's too complicated to extract the demo from my project, connected to the database.
But I can confidently say that if the service returns a Tstream, the response is NOT compressed.
I made 2 identical services, one that returns a Tstream and the other that returns a String.
The first does not return the compressed response, while the second respects gzip compression perfectly.
So it is certain that a response with Tstream is not compressed.
Should I consider this as a bug to be fixed?
Otherwise, can you tell me what I can use, components/functions already present in Xdata/Sparkle to compress the streams?
(which are actually ready-made json).
Thanks and good work!
If you're interested, part of a blog post I wrote a while ago covered how to add Brotli compression. I was after this particularly for the better compression rate when compressing JSON. The Actorious website has a lot of JSON, and this reduced the size of the transfers considerably, even compared to gzip.
Have you paid attention the three points I made in my very first answer to this topic, especially item 3?
- Response content-type is "compressible" (most text types, like text/html, text/plain, application/json, etc.
When XData returns a TStream, you must set the content-type
yourself: Service Operations | TMS XData documentation.
Uhm...
Ok, I will try setting content-type to 'Application/json'.
Thanks
Ok, it work.
The TXDataOperationContext.Current.Response.ContentType:= 'application/json' compression assignment was successful.
I instead focused on Accept-encoding and ContentEncoding :-)
Ok, thanks
I take this opportunity to make 2 requests:
- The ability to add control over the maximum length of strings in entity generation (as an option in Aurelius, in IDE). Currently I can only do this with the script in Data Modeler.
- The possibility of intercepting the SQL code generated by the Aurelius Linq in "simulation", time after time. For example, I need the select SQL code to make calls to the database and convert the result directly into json, without going through the entities, when I need a large amount of data and very quickly (in this case the orm is rather slow).
But I have to say that I'm starting to like Xdata :-)
I will close this topic with the correct solution, then could you please open separate topics for the respective discussions?
ok, thanks
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.