Note that there are some explanatory texts on larger screens.

plurals
  1. POUncompress gzipped http request body to json in Node.js
    primarykey
    data
    text
    <p>I have a windows 8 application connecting to a web service written in Node.js. On the windows 8 side I compressed my request body to gzip. But on the Node.js side I found that my req.body type was <code>Object</code>. </p> <p>I cannot use <code>zlib</code> to uncomporess the body since it's not a <code>stream</code>.</p> <p>I can use <code>zlib</code> to uncomporess the req, but I don't know how to retrieve the <code>req.body</code> content from the unzipped stream and parse the body in JSON format.</p> <p>BTW, I reviewed my request through Fiddler and it told me the request body was gzipped, and I can see my raw body through Fiddler after unzipped so the request should be correct.</p> <p><strong>Updated</strong> Below is my Node.js app</p> <pre> (function () { var express = require("express"); var zlib = require("zlib"); var app = express(); var port = 12345; app.configure(function () { app.use(express.compress()); app.use(express.bodyParser()); }); app.post("/test", function (req, res) { var request = req.body; req.pipe(zlib.createGunzip()); var response = { status: 0, value: "OK" }; res.send(200, response); }); console.log("started at port %d", port); app.listen(port); })(); </pre> <p>And below is my windows store app code (partial)</p> <pre> private async void button1_Click_1(object sender, RoutedEventArgs e) { var message = new { Name = "Shaun", Value = "12345678901234567890123456789012345678901234567890" }; var json = await JsonConvert.SerializeObjectAsync(message, Formatting.Indented); var bytes = Encoding.UTF8.GetBytes(json); var client = new HttpClient(); client.BaseAddress = new Uri("http://192.168.56.1:12345/"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.ExpectContinue = false; var jsonContent = new JsonContent(message); var gzipContent = new GZipContent3(jsonContent); var res = await client.PostAsync("test", gzipContent); var dialog = new Windows.UI.Popups.MessageDialog(":)", "完成"); await dialog.ShowAsync(); } internal class GZipContent3 : ByteArrayContent { public GZipContent3(HttpContent content) : base(LoadGZipBytes(content)) { //base.Headers.ContentType = content.Headers.ContentType; base.Headers.ContentType = new MediaTypeHeaderValue("x-application/x-gzip"); base.Headers.ContentEncoding.Add("gzip"); } private static byte[] LoadGZipBytes(HttpContent content) { var source = content.ReadAsByteArrayAsync().Result; byte[] buffer; using (var outStream = new MemoryStream()) { using (var gzip = new GZipStream(outStream, CompressionMode.Compress, true)) { gzip.Write(source, 0, source.Length); } buffer = outStream.ToArray(); } return buffer; } } internal class JsonContent : StringContent { private const string defaultMediaType = "application/json"; public JsonContent(string json) : base(json) { var mediaTypeHeaderValue = new MediaTypeHeaderValue(defaultMediaType); mediaTypeHeaderValue.CharSet = Encoding.UTF8.WebName; base.Headers.ContentType = mediaTypeHeaderValue; } public JsonContent(object content) : this(GetJson(content)) { } private static string GetJson(object content) { if (content == null) { throw new ArgumentNullException("content"); } var json = JsonConvert.SerializeObject(content, Formatting.Indented); return json; } } </pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload