Skip to content

Instantly share code, notes, and snippets.

@notionparallax
Last active September 30, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notionparallax/8dde9a2daee3dbe090bbb4eafb95ee7f to your computer and use it in GitHub Desktop.
Save notionparallax/8dde9a2daee3dbe090bbb4eafb95ee7f to your computer and use it in GitHub Desktop.
var zlib = require("zlib");
var rawParser = bodyParser.raw({type: '*/*'});
app.post('/zipped', rawParser, function(req, res) {
// console.log('here we go');
// console.log('body', req.body);
// console.log('body', req.headers);
// console.log('body type', typeof req.body);
// console.log('req body len', req.body.length);
if(req.headers["content-type"]){
if(req.headers["content-type"] === "application/octet-stream"){
zlib.gunzip(req.body, function(err, buf) {
if(err){
console.log("err:", err );
res.status(666).send("failed to unzip");
} else{
console.log("in the inflate callback:", buf);
console.log("to string:", buf.toString("utf8") );
res.status(200).send({"message":"rad, it worked zipped","data":buf.toString("utf8")});
}
});
} else if(req.headers["content-type"] === "application/json"){
console.log(req.body);
res.status(200).send({"message":"rad, it worked as json","data":req.body});
}
} else {
res.status(666).send("failed. No headers?");
}
});
import json
import requests
import logging
from httplib import HTTPConnection
import StringIO
import gzip
HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
url = "http://localhost:3000"
headers = {"Content-Type":"application/octet-stream"}
data = [{"key": 1,"otherKey": "2"},{"key": 3,"otherKey": "4"}]
payload = json.dumps(data)
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode="w") as f:
f.write(json.dumps(data))
out.getvalue()
print "\ntry a zipped body"
rZipped = requests.post(url+"/zipped",
data=out.getvalue(),
headers=headers)
print rZipped.status_code, rZipped.text
print "\ntry a plain body"
headers = {"Content-Type":"application/json"}
rNotZipped = requests.post(url+"/zipped",
data=payload,
headers=headers)
print rNotZipped.status_code, rNotZipped.text
print "\ntry a plain body"
rNotZipped = requests.post(url+"/zipped",
data=payload,
headers={})
print rNotZipped.status_code, rNotZipped.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment