I'm trying to retrieve data from Keepa using express js and HTTP module,
Keepa sending all data as gzip.
I was challenged to get the data properly and I got a previous error-Unexpected token in JSON at position 0,
So I have installed 'decompress-response' module which resolved this issue but now I'm getting half of the JSON data and then a new syntax error appears - unexpected end of json input Node Js
I'm trying to figure what am I missing here.. hope you can help me.
const express = require("express");
const https = require("https");
const decompressResponse = require("decompress-response");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
console.log(req.body.asinId);
const query = req.body.asinId;
const apiKey = "MY_API_KEY";
const url = "https://api.keepa.com/product?key="+ apiKey +"&domain=1&asin="+ query;
https.get(url, function(response){
response = decompressResponse(response);
console.log(response.statusCode);
console.log(response.headers);
var data;
response.on("data", function(chunk) {
if (!data) {
data = chunk;
} else {
data += chunk;
}
const asinData = JSON.parse(data);
console.log(asinData);
res.send();
});
});
});
Please try to print the response before "response = decompressResponse(response);". And let me know what you get there.
Related
I'm trying to retrieve data from KEEPA about Amazon's products.
I'm straggling to receive the data in proper JSON format, as KEEPA sending the data as gzip.
I tried to used 'decompressResponse' module which helped to get the data as JSON but it was received multiple times on each call.
As the code appears below I'm just getting a huge Gibberish to my console.
Let me know what am I missing here, or if you have a better suggestion please let me know.
Thanks
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res) {
const query = req.body.asinId;
const apiKey = "MY_API_KEY";
const url = "https://api.keepa.com/product?key=" + apiKey + "&domain=1&asin=" + query;
const options = {
methode: "GET",
headers: {
"Content-Type": "application/json;charset=UTF-8",
"Accept-Encoding":"gzip"
}
}
https.get(url,options,function(res) {
console.log(res.statusCode);
console.log(res.headers);
var data;
res.on("data", function(chunk){
if(data){
data = chunk;
} else {
data += chunk;
}
console.log(data);
});
});
res.send("server is running");
});
app.listen(3000, function() {
console.log("server is running on port 3000");
});
Have you tried using the built-in zlib module with gunzip()?
zlib.gunzip(data, (error, buff) => {
if (error != null) {
// An error occured while unzipping the .gz file.
} else {
// Use the buff which contains the unzipped JSON.
console.log(buff)
}
});
Full example with your code: https://www.napkin.io/n/7c6bc48d989b4727
well the output function was wrong .. the correct one below
https.get(url,options,function(response) {
response = decompressResponse(response);
console.log(res.statusCode);
console.log(res.headers);
let data = '';
response.on("data", function(chunk){
data += chunk;
});
response.on("end",function(){
console.log(data);
});
});
So I am making an API to check if a Minecraft server is online or not. I am using express and bodyParser and using a JSON file to store the status. I am sending a POST request to the server and want to make sure that the POST request body is sending the right key.
This is my current code:
var fs = require('fs');
var data = fs.readFileSync('serverStatus.json');
var status = JSON.parse(data);
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const cors = require('cors');
app.get('/status', alldata);
function alldata(request, response) {
response.send(status);
}
app.post('/status', (req, res) => {
if (!req.is('application/json')) {
res.status(500);
res.send('500 - Server Error');
} else {
res.status(201);
res.send(req.body);
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`Server running on: http://localhost:${PORT}`)
);
and this is my test JSON file:
{
"survival": "online",
"creative": "online"
}
Is there also an easy way to update the JSON file to edit the original server statuses?
Thanks in advance!
You can access the values in the JSON object using the object's keys:
req.body.key
or
req.body['key']
If the JSON object doesn't contain that key, it will return undefined
To assign a value, you can use the same logic:
req.body.key = value
This will update the original object, so you can return req.body and it will contain the new value.
Trying to retrieve information of any Github profile with the help of this API:
Github API - https://api.github.com/users/{username of any Github account}
const express = require("express");
const app = express();
const https = require("https");
const bodyParser = require("body-parser");
const request = require("request");
app.listen(3000 , function(){
console.log("Server started at port 3000");
});
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.get("/" , function(req , res){
res.sendFile(__dirname +"/index.html" );
});
app.post("/" , function(req , res){
const user = req.body.username;
const url = "https://api.github.com/users/"+user ;
https.get(url , function(response){
response.on("data", function(data){
const temp = JSON.parse(data);
const bravo = temp.login;
res.write("<p>The login of this Github ID is " + bravo + "<p>" );
});
});
console.log("Post request received.");
});
Getting error in my CLI stating user agent error and unexpected token R in JSON at position 2
The target of your requests won't return in json format if there is a space or other invalid character in the uri you call.
calling with "testme" as your bravo variable rendered json but "testme please" returned html.
I think if you log data before trying to parse it as json you will find you're being served html.
I have the following code:
var getRequest=function(options){
var body='';
var req = http.get(options,function(response){
response.on('data', function(chunk) {
body+=chunk;
}).on('end', function(chunk) {
console.log("body");
return JSON.parse(body);
}).on('error',function(error){
console.log("error: "+error.getMessage());
})
})
return req;
};
What I am trying to do is pass a JSON object of http options,eg:
var options={
host:'localhost',
port:'8080',
method:'GET',
path:'/stuff'
};
and send back a parsed response. However, I can't get this to work and I think its because of the nested function and my misunderstanding of how they work.
Please could someone advise me on how to get the function to return the result of JSON.parse(body) to getRequest?
TypeError: Converting circular structure to JSON
thanks.
Easy way to do it is with a body-parser middleware:
Install body-parser with npm (npm install body-parser --save)
Use it like this
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Now you got access to the body object in request.body
app.get('/stuff', function(request, response) {
response.send(request.body);
}
If you want to handle the body packages yourself, here is a sample of working code.
var data = [];
request.on('data', function(chunck) {
data.push(chunck);
}).on('end', function() {
data = Buffer.concat(data).toString();
response.send(JSON.parse(data));
}).on('error', function(error) {
console.log(error);
response.status(400).send(error);
});
I am using node js with express framework and rest api
for rest api client i am using postman extension with chrome browser
here i am able to get values from option "x-www-form-urlencoded" but i am not able to get values from "form data" i want to get values from "form data" option and also need to upload image file.
please help me any to achieve this. i want to get values from "form data" option and also image. please help me.
Below i have mentioned code what i have used.
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var http = require('http').Server(app);
var mysql = require('mysql');
var util = require('util');
var trim = require('trim');
var validator = require('validator');
var bodyParser = require('body-parser');
var Ingest = require('ingest');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var type = upload.single('recfile');
passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
async = require('async');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/upload', function(req, res){
console.log(req.file); // "form-data" values not able to get here
console.log(req);// "form-data" values not able to get here
console.log('body : '+JSON.stringify(req.body));// "form-data" values not able to get here
});
i didn't set any content type in postman header
app.post('/upload', function(req, res){
console.log('req.headers \n '+JSON.stringify(req.headers));
console.log('req.body.file :- '+req.body.file);
console.log('\n\n req.body :- '+JSON.stringify(req.body));
});
I got the below result for the above code.
req.headers
{"host":"localhost:3001","connection":"keep-alive","content length":"5808","cache-control":"no-cache","origin":"chrome-extension://mkhojklkhkdaghjjfdnphfphiaiohkef","password":"password","user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36","username":"User2","content-type":"multipart/form-data; boundary=----WebKitFormBoundaryV4zAIbjEyKYxLRWe","accept":"*/*","accept-encoding":"gzip, deflate","accept-language":"en-US,en;q=0.8","cookie":"connect.sid=s%3Atz4f1ZgJkaAjuDD1sOkMB9rr.Z8EUIyxEcr0EyFQL96v0ExGRidM3SAVTx8IIr52O0OI"}
req.body.file :- undefined
req.body :- {}
Yes the same problem facing me several times and according my experience you do't set the content-type in postman header because it should be undefined and node server automatically set the content type according to requirement. If you set the content-type then you do't get any image and other data in the node server .
You get the image from the req.body.file
you get the other data from req.body
app.use(multipart()) in middleware
Procedure how to use multipart as middleware
var multipart = require('connect-multiparty');
global.app = module.exports = express();
app.use(multipart());
I got solution with help of the below code
var express = require('express');
var router = express.Router();
var util = require("util");
var fs = require("fs");
var formidable = require('formidable');
var path = require('path');
router.post("/upload", function(req, res, next){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
// `file` is the name of the <input> field of type `file`
console.log(files);
console.log(fields);
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('error', function(err) {
console.error(err);
});
form.on('progress', function(bytesReceived, bytesExpected) {
var percent_complete = (bytesReceived / bytesExpected) * 100;
console.log(percent_complete.toFixed(2));
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = 'public/images/';
fs.readFile(temp_path, function(err, data) {
fs.writeFile(new_location + file_name, data, function(err) {
fs.unlink(temp_path, function(err) {
if (err) {
console.error(err);
} else {
console.log("success!");
}
});
});
});
});
});
I think you need to use body-parser for this and also need to update your app.js as
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
I was facing same problem I was not able to get form data fields body parser is not enough to get those values. Hitting google searched a lot of stuff about it but nothing works. Here is the solution how I got form data values.
Note: I am using typescript instead of js
Install multer package: npm i multer
Now in app.ts or app.js import accordingly:
import multer from "multer"
Define the multer function
const upload = multer(); // config
After body parser:
app.use(upload.any());