Gibrish when fetching an image and sending to client - node.js

In my server i read an image - jpg - from a url with :
let _uri = "https.....xxxxxxxxxxx.jpg";
let _headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': '*',
'Accept-Language': 'en-US,en',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Referer': 'http://www.google.com/',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
var options = {
uri: _uri,
headers: _headers
};
rp(options)
.then(parsedBody => {
//response.setHeader('Content-Type', 'image/jpg');
// return response.status(200).send(parsedBody); //** don't work also
return response.json(parsedBody);
})
.catch(err => {
response.status(400).send(err)
});
});
No matter what i try, this will get a huge chunk of garbage when i check the response.
I can't find out what type of data is it.
On client i receive it - again - a lot of jibrish :
fetch('https://...my server')
.then(res => res.blob())
.then(blob => {
console.log(blob);
How do i get this photo on server and send to client ?

Add an option encoding: null to have the response returned as Buffer
var options = {
uri: _uri,
headers: _headers,
encoding: null // add this line
}
Then you can just send the buffer as response
rp(options)
.then(buffer => {
response.setHeader('Content-Type', 'image/jpg');
return response.status(200).send(buffer);
})

Related

How to upload multipart/form-data with axios?

I try to upload to file hosting, i debug it in firebug. i match all thing with disable javascript condition. it return empty data. in debug, it should return page that contain the file link.
const axios = require('axios').default;
const fs = require('fs');
const FormData = require('form-data');
let formData = new FormData();
formData.append('file_0', fs.readFileSync('filepath');
const fileSize = fs.statSync('filepath').size;
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
axios({
method: 'post',
url: 'https://www55.zippyshare.com/upload',
headers: {
'user-agent': userAgent,
'content-length': fileSize,
'host': 'www55.zippyshare.com',
'origin': 'https://www.zippyshare.com',
'referer': 'https://www.zippyshare.com/',
'connection': 'keep-alive',
'content-type': 'multipart/form-data',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
},
data: formData
}).then(res => {
console.log(res.data)
})

Axios connection is not letting Jest gets terminated

I am new in Node/Jest world. I am trying to test the following function:
export async function visitPage(url: string) {
const cookie = ''
let headers = {
'authority': 'www.example.com',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'upgrade-insecure-requests': '1',
'dnt': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'rtt': '350',
'downlink': '1.45',
'ect': '3g',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'referer': 'https://google.com',
'accept-language': 'en-US,en;q=0.9,ur;q=0.8,zh-CN;q=0.7,zh;q=0.6',
'Accept-Encoding': 'gzip',
'cookie': cookie,
}
try {
const response = await axios({
method: 'get',
url: url,
headers: headers
});
if (response.status == 200) {
let html = response.data
if(html.includes('Hello, Sign in')) {
// console.log('Not logged in')
return false
}
}
} catch(error) {
if (error.response) {
console.log(error.response.data);
}
}
return true;
}
In test I am doing like the below:
describe('Product', () => {
it('visitPage', async () => {
const pageData = await visitPage('https://example.com');
// console.log(pageData)
expect(pageData).toBe(false);
});
});
The test gets passed but then it prints the following:
an all test suites.
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TLSWRAP
31 |
32 | try {
> 33 | const response = await axios({
| ^
34 | method: 'get',
35 | url: url,
36 | headers: headers
Use nextTick before axios call as stated in here
await process.nextTick(() => {})
That solves my issue.

Logging into a website once and reusing the session using Request

I'm using request.
I want to login a website only once, then use the same connection until it expires, so I won't have to login every time I send a request.
How can I do that?
I have tried the code below, but it doesn't retain the session. I'm using Redis to save cookie values, this is probably the wrong way, though. Two cookies are generated after you log in.
var request = require("request").defaults({jar: true, followAllRedirects: true});
var options1 = {
method: 'GET',
url: 'homepageURL',
headers:
{
'cache-control': 'no-cache',
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
referer: 'URL',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'upgrade-insecure-requests': '1'
}
};
request(options1, function (homeError, homeResponse, homeBody) {
if (homeResponse.statusCode == 200) {
var form = {"username": "myusername", "password": "mypassword", "secret": "mysecret", "action": "login"};
var options2 = {
method: 'POST',
url: 'siteurl + "/" + login.asp',
headers:
{
'cache-control': 'no-cache',
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
referer: 'URL',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'upgrade-insecure-requests': '1',
origin: url + "/" + login.asp
},
form: form
};
request(options2, function (loginError, loginResponse, loginBody) {
if (loginResponse.statusCode == 200) {
console.log("Successfully logged in.");
client.set("cookie1", homeResponse.request.headers.cookie, function () {
client.set("cookie2", loginResponse.request.headers.cookie, function () {
nrp.emit('profile', {});
});
});
}
});
}
});
Profile test
nrp.on('profile', function () {
client.get("cookie1", function (cookie1Err, cookie1) {
client.get("cookie2", function (cookie2Err, cookie2) {
var cookie = cookie1 + ";" + cookie2;
var options = {
method: 'GET',
url: 'site/profile',
headers:
{
'cookie': cookie,
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'upgrade-insecure-requests': '1'
}
};
request(options, function (error, response, body) {
console.log(body);
// it says you are not logged in, but if you use this inside the block above, it works.
});
})
});
});

set Content-Type to json but request comes in as x-www-form-urlencoded

I am using ajax to send post to my app. In postman everything works as expected, however in browser, data comes in as x-www-form-urlencoded and data is missing.
Below is my ajax and postman configurations and their corresponding headers in req...
$.ajax({
url: '/',
headers: {
'Accept': 'application/json',
'Content-Type': "application/json"
}, // send as JSON
data: JSON.stringify(formData),
method: 'POST'
});
headers:
{ host: 'localhost:8080',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost:8080/',
'content-type': 'application/x-www-form-urlencoded',
'content-length': '0',
connection: 'keep-alive',
'upgrade-insecure-requests': '1',
pragma: 'no-cache',
'cache-control': 'no-cache' }
headers:
{ data: '{"name":"tester", "email": "t#t.com", "message":"This is a test"}',
contenttype: 'application/json',
datatype: 'xml/html/script/json',
'cache-control': 'no-cache',
'postman-token': 'cbaa5cbe-0ab8-4328-abfd-f5dc6a6acd90',
'user-agent': 'PostmanRuntime/7.1.1',
accept: '*/*',
host: 'localhost:8080',
'accept-encoding': 'gzip, deflate',
'content-length': '0',
connection: 'keep-alive' }
also my relevant server code:
app.use(bodyParser.json());
//handle urlencoded data
app.use(bodyParser.urlencoded({extended:false}));
app.post('/', function (req, res) {
console.log(res);
console.log('----------------------------------------------------------');
console.log(req);
//var jsonData = JSON.parse(req.headers.data);
console.log(req.body);
console.log(req.data);
let transporter = courier.createTransport({...});
let mailOptions = {...};
transporter.sendMail(mailOptions, function (error, info) {
if (error)
console.log(error);
else
console.log('email sent:' + info.response);
});
res.render('index', {
title: 'Projects',
projects: projects
});
});
app.get('/', function (req, res) {
res.render('index', {
title: 'Projects',
projects: projects
});
});
plz halp!
on you can just simply:
JSON.parse(data)
inside your
success: function(data){
var object =JSON.parse(data);
}
once the json parses the data you can do what ever you like.

the http.request return a wrong statusCode?

I use this code:
var options = {
hostname: 'torcache.net',
path: '/torrent/4C1E01E1E7AE19082F4BAC9C3C82B5B2CD0EEA23.torrent',
port: 80,
method: 'GET'
};
http.request(options, function (res) {
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function () {
if (res.statusCode !== 200) {
callback(res.statusCode);
return;
}
//...
});
}).on('error', function (err) {
callback(err);
}).end();
the statusCode is 200, but open the url(http://torcache.net/torrent/4C1E01E1E7AE19082F4BAC9C3C82B5B2CD0EEA23.torrent) is Chrome, the statusCode is 404. what's wrong with the code?
update:
I find if I use this options:
{
hostname: 'torcache.net',
path: '/torrent/4C1E01E1E7AE19082F4BAC9C3C82B5B2CD0EEA23.torrent',
port: 80,
method: 'GET',
secureProtocol: 'SSLv3_method',
headers: {
'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'accept-language': 'en-US,en;q=0.8',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
'accept-encoding': 'gzip,deflate',
'referer': 'http://torcache.net' // !!!
}
}
when the headers['referer'] is not null, the res.statusCode is wrong. but I don't know why?
The request does indeed return with a 200. It is later redirect with JavaScript to a 404 page, which Node doesn't follow.

Resources