Koajs and curl with Content-Encoding: gzip,deflate - node.js

I am looking for simple example of exchange data between client (using curl) and Koajs server with Content-Encoding: gzip,deflate.
More detail: the client have log file. Client compress the log file and send the log file to server. Server decompress the log file and get the content.
Please give me both curl command and Koajs code.
Thank you.

When you wanna send compressed request to server using curl you can do so using
curl -v -H "Content-encoding: gzip" -X POST -H 'Content-Type:
application/json;charset=UTF-8' --data-binary #youdataFile.gzip
your_endpoint.com
and if you wanna decompress that request in koaServer, I don't really know about that but would surely like to know. As even I'm in need to know that

This is how I decompress data in Koa Server using zlib
var zlib = require('zlib');
zlib.unzip(buffer, (err, buffer) => {
if (err) return console.log(err);
var dataString = buffer.toString();
});

Related

Erroneous newline only when executing a curl request with nodejs app

I have a simple Nodejs server using express.
// App.js
const express = require("express");
const app = express();
app.get("/", (req, res, next) => {
res.status(200).send("Foo");
});
app.listen(3001);
When I am visiting localhost3001 in Chrome I see the anticipated output.
When I execute curl --request GET --write-out "%{http_code}\n" http://localhost:3001/ I get...
curl --request GET --write-out "%{http_code}\n" http://localhost:3001/
Foo200
When I was anticipating...
curl --request GET --write-out "%{http_code}\n" http://localhost:3001/ Foo200
I sent the output to a text file and observed no newline. Is there anyway to supress this newline?

Sever returning 400 when body passed with GET

I have an Express GET route to fetch all users. Whenever a req.body is present, the server is always responding with 400.
Obviously, I have body-parser and this behaviour is only experienced in the production k8s environment (working fine in localhost)
Is there any limitation to send req.body to GET routes? Express documentation doesn't say so
You need to set the content type --header "Content-Type: application/json" in the GET request and it should work.
curl --header "Content-Type: application/json" --request GET --data '{"username":"xyz","password":"xyz"}' https://example.com/posttest
and here is the nodejs snippet
app.get('/posttest', function(req, res){
console.log("posttest call received")
res.send(req.body);
});

curl command equivalent in nodejs

I have the curl command here which is working as I want:
curl -X POST -H "Content-Type: application/json" -d #cats.json http://localhost:8080
And I would like to get help on translating this into nodejs
I currently have localhost set up and my goal is to display data stored in a JSON file. I currently copy and paste the data into postman and post the data to the localhost but I want to not use postman. The curl command was useful but I want to incorporate it into my code.
I've tried
var request = require('request');
request.post({
headers: {'content-type' : 'application/json'},
url: 'http://localhost/8080',
body: "someData"
}, function(error, response, body){
console.log(body);
});
I've tried this but it doesn't provide the same outcome as the curl command does where the data appears in my localhost
In postman, you can export NodeJS request code out:
Click on Code
Then select NodeJS -> Request.
Copy to Clipboard

Nodejs request behaves differently than Postman and Curl

I've been messing with that for a while now and can't seem to figure it out... I'm trying to send a POST request to a device (what device doesn't matter I guess) in order to change it's IP address. Usually, the setup is done through the browser, so I'm trying to emulate that request. It seems to be working fine when I make the request with Postman or Curl but not with the request module from Node.js.
All requests return a 200 OK status code but the latter seems to be not accepted. This is the request I perform using Postman:
POST /configuration_tab/ajax_comb_table_save/network_config/network_config_schema HTTP/1.1
Host: 192.168.1.250
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: PHPSESSID=mg37okvhkr1eqv1okq99i98i62
Cache-Control: no-cache
Postman-Token: 234ece69-c3f8-2d29-34a5-c40f8e64f708
form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26
IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26
IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26
SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable
This is what I sent through curl:
curl -X POST "http://192.168.1.250/configuration_tab/ajax_comb_table_save/network_config/network_config_schema"
-H "Content-Type:application/x-www-form-urlencoded; charset=UTF-8"
-H "Cookie:PHPSESSID=mg37okvhkr1eqv1okq99i98i62"
-d "form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable"
Again, both of them work fine. Now, this is the code I was using to try the same thing via Node:
let body = "form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable"
request({
url: "http://192.168.1.250/configuration_tab/ajax_comb_table_save/network_config/network_config_schema",
method: "POST",
headers: {
"Cookie": "PHPSESSID=mg37okvhkr1eqv1okq99i98i62",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: body
}, (err, resp) => {
// Do whatever...
});
Does anyone have an idea what might be different or anything I'm missing? Any help would be appreciated.

Self-hosted Parse Platform RESTful API access

I'm using Parse for some quick MVP app development to prove some concepts. Everything is working fine and I'm simply trying to extend the abilities of our app by setting up some 3rd party integrations etc.
I would like to access my Cloud Code functions via the RESTful api, as documented here
I've changed the server address, but have no luck connecting and pulling the data.
I am using Node.js to connect to the REST api, which is being hosted on Heroku.
var options = {
host: 'https://###############.herokuapp.com',
port: 443,
path: '/parse/functions/'+req.param('text'),
method: 'POST',
headers: {
'X-Parse-Application-Id': '##############',
'X-Parse-REST-API-Key': '###########'
}
};
https.get(options, function(resp){
resp.on('data', function(chunk){
//do something with chunk
res.send(chunk)
});
}).on("error", function(e){
console.log("Got error: " + e.message);
res.send(e)
});
So far no luck. Any suggestions?
Hard to tell what exactly is wrong without seeing the Parse Server configuration, the Cloud Code function, and more importantly the server's response, but here's what worked for me:
curl -X POST -H "X-Parse-Application-Id: XYZUASDASDA" -H
"X-Parse-REST-API-Key: XYZUASDASDA" -H "Content-Type:
application/json" -d '{}'
http://localhost:3000/parse/functions/averageStars
Result: {"result":"Hello"}
Here's my test Cloud Code function:
Parse.Cloud.define('averageStars', function(request, response) {
response.success('Hello'); });
As a quick test, I suggest:
Try using CURL
Try the test function above

Resources