Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST request with GZIPPED json not working with request.AddJsonBody #1438

Closed
driekus77 opened this issue Mar 18, 2020 · 7 comments
Closed

POST request with GZIPPED json not working with request.AddJsonBody #1438

driekus77 opened this issue Mar 18, 2020 · 7 comments

Comments

@driekus77
Copy link

Expected Behavior

GZIP Compression with POST working in combination with request.AddJsonBody.

Actual Behavior

Get a Gateway timeout when using request.AddJsonBody in combination with Content-Encoding: gzip.

Works when I gzip the serialized json myself and post it. (See beneath code).

Steps to Reproduce the Problem

This code is working:

	...
	IRestResponse restResponse;

	var dataStream = new MemoryStream();

	using (var zipStream = new GZipStream(dataStream, CompressionMode.Compress))
	using (var writer = new StreamWriter(zipStream))
	{
	    writer.Write(serializedBody);
	}

	var compressedBytes = dataStream.ToArray();

	request.AddHeader("Content-Encoding", "gzip");
	request.AddHeader("Content-Type", "application/json");
	request.AddParameter("application/x-gzip", compressedBytes, ParameterType.RequestBody);

	restResponse = client.Post(request);
	...

Fiddler's raw request header:

Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/106.0.0.0
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Content-Encoding: gzip
Content-Type: application/json
Content-Length: 524946

and raw response header:

HTTP/1.1 200 OK
x-powered-by: Express
x-request-id: k7wm6l5h
x-frame-options: SAMEORIGIN
content-type: application/json; charset=utf-8
etag: W/"xxx-xxx"
date: Wed, 18 Mar 2020 00:55:36 GMT
connection: close
vary: Accept-Encoding
content-encoding: gzip
set-cookie: JSESSIONID=xxx; Path=/; HttpOnly; Secure
transfer-encoding: chunked

This code is NOT working:

	...
    request.JsonSerializer = JsonResultSerializer;

    // Note: When I comment beneath line out it works as normal. (But without compression...)
    request.AddHeader("Content-Encoding", "gzip"); 

    request.AddJsonBody(vergoedingSetQueue);

   	restResponse = client.Post(request);
	...    

Fiddler's raw request header:

Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/106.0.0.0
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Content-Encoding: gzip
Content-Type: application/json
Content-Length: 8581208

NOTE: JSon shows up hier...!

Raw response header:

HTTP/1.1 504 Gateway Timeout
x-powered-by: SAP Application Controller
x-request-id: xxx
x-frame-options: SAMEORIGIN
cache-control: no-cache, no-store, must-revalidate
set-cookie: JSESSIONID=xxx; Path=/; HttpOnly; Secure
date: Wed, 18 Mar 2020 01:08:05 GMT
connection: close
transfer-encoding: chunked

f
Gateway Timeout
0

Specifications

  • Version: RestSharp 106.10.1
  • Platform: Windows 10(Client) with dotnet core 2.2
  • Subsystem: Server side is SAP XSA with Node JS Expressjs service
@alexeyzimarev
Copy link
Member

RestSharp wasn't designed to compress the payload. AddJsonBody just instructs the RestClient to call an appropriate serializer and also set the correct content type.

So, it is not a bug and there's no word in the docs that give a clue that it would compress the payload based on the content type set in the header.

It can be done very easily by creating a custom serializer https://restsharp.dev/usage/serialization.html#custom

So, I can mark it up for grabs, if you are willing to contribute.

@driekus77
Copy link
Author

Thank you for quick response!

Ah me not reading & interpreting the docs oke: Sorry.

Can try to make a PR for this with a GZipSerializer that can be decorated with an JSonSerializer.

For the time being as an optional class to be set if wanted?

@driekus77
Copy link
Author

Looked at the code and found out that its not a trivial task to introduce Compression using the IRestSerializer/ISerializer/IDeserializer interface(s).
In order not to break anything either these Interfaces must be extended using

byte[] Serialize(object payload); //  in stead of string. 
// or
void Serialize(Stream stream); 

Or introduce a new Interface for e.g. "RestSharp.Sinks" with an Interface that supports Stream and/or byte[].

So extend existing Interfaces or add a new one?
What do we prefer?

Kind regards

@alexeyzimarev
Copy link
Member

I would prefer that serializers will use byte arrays but it will be a breaking change, so I'll have to merge it after the last minor release of v106 so we can take it in to v107.

@driekus77
Copy link
Author

Oke I will give it a go.

Thanks for replying quickly!

@stale
Copy link

stale bot commented Jun 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 16, 2020
@repo-ranger
Copy link

repo-ranger bot commented Jun 16, 2020

⚠️ This issue has been marked wontfix and will be closed in 3 days

@repo-ranger repo-ranger bot closed this as completed Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants