Here is my code. I am running 0.10.24 version. Can someone look into this? An error, "Error: 6304:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:766:" is displayed.
var url = require('url'),
request = require('request'),
loginLink = "https://foo";
var params = {
maxRedirects: 10,
followRedirect: true,
followAllRedirects: true,
timeout: 10000,
url: url.parse(loginLink),
method: "POST",
jar: true,
strictSSL: false,
form: {
'userid': 'userid',
'pwd': 'pass',
'timezoneOffset': -330
},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1753.0 Safari/537.36',
'Connection': 'keep-alive',
'Origin': 'https://foo'
}
}
request(params,function(error,response,body){
if(!error && response.statusCode == 200){
console.log(body);
}else{
console.log(response);
console.log(error);
}
});
change SSL version (?)
options = {
....
strictSSL: false,
secureProtocol: 'SSLv3_client_method',
....
}
Related
I use Replit Nodejs
thats about dlive bot, The resource I found about the dlive api was very insufficient for me, so I am having a hard time.
async function sendMessage(message){
let url = 'https://graphigo.prd.dlive.tv'
let headers = {
'accept': '*/*',
'authorization': authKey,
'content-type': 'application/json',
'fingerprint': '',
'gacid': 'undefined',
'Origin': 'https://dlive.tv',
'Referer': 'https://dlive.tv/'+displayname,
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
let data = {
'operationName': 'SendStreamChatMessage',
'variables': { "input":
{
'streamer': blockchainUsername,
'message': message,
'roomRole': 'Moderator',
'subscribing': true
}
},
'extensions':{
"persistedQuery":{
"version":1,
"sha256Hash":Hash
}
},
}
response = await axios.post(url, data=JSON.stringify(data), headers=headers)
console.log(response.data)
}
sendMessage("test!")
Console:
{ errors: [ { message: 'Require login', path: [Array] } ], data: null }
request get no body content with header "content-length: 0".
resource: https://www.doe.gov.taipei/OpenData.aspx?SN=8A3B3293C269E096
It is a rss link, work well in browser and curl.
curl test on linux, and wget works too.
here is simple code:
const req = require('request');
req.get({
url: 'https://www.doe.gov.taipei/OpenData.aspx?SN=8A3B3293C269E096',
rejectUnauthorized: false
}, (err, res, body) => {
console.log(res)
});
test it online simulator get same result
https://repl.it/repls/ScalyAquamarineObjectpool
i think it should be a text/xml content, not empty.
response:
{
statusCode: 200,
body: '',
headers:
{ 'cache-control': 'private,No-cache',
'set-cookie':
[ 'ASP.NET_SessionId=kzdhyxq2grttp5awwpqzxyen; path=/; secure; HttpOnly' ],
'x-frame-options': 'SAMEORIGIN',
'strict-transport-security': 'max-age=0',
'x-xss-protection': '1; mode=block',
'x-content-type-options': 'nosniff',
'content-security-policy':
'frame-ancestors \'self\' https://www-mgr.gov.taipei http://www-mgr.gov.taipei',
date: 'Wed, 03 Apr 2019 06:44:47 GMT',
connection: 'close',
'content-length': '0' },
request:
{ uri:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.doe.gov.taipei',
port: 443,
hostname: 'www.doe.gov.taipei',
hash: null,
search: '?SN=8A3B3293C269E096',
query: 'SN=8A3B3293C269E096',
pathname: '/OpenData.aspx',
path: '/OpenData.aspx?SN=8A3B3293C269E096',
href:
'https://www.doe.gov.taipei/OpenData.aspx?SN=8A3B3293C269E096' },
method: 'GET',
headers: {} }
}
The signature of your callback function is wrong. In Node.JS, usually the first parameter to the callback is the (optional) error object.
Try this:
const req = require('request');
req.get({
url: 'https://www.doe.gov.taipei/OpenData.aspx?SN=8A3B3293C269E096',
rejectUnauthorized: false
}, (err, res, body) => {
console.log(res)
});
To your updated question: It seems like adding a user-agent does the trick:
const req = require('request');
req.get({
url: 'https://www.doe.gov.taipei/OpenData.aspx?SN=8A3B3293C269E096',
rejectUnauthorized: false,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3750.0 Iron Safari/537.36'
}
}, (err, res, body) => {
console.log(body)
});
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.
});
})
});
});
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.
I have code like the following:
var req = require('request');
req.post('someUrl',
{ form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'}, },
function (e, r, body) {
console.log(body);
});
How can I set headers for this?
I need user-agent, content-type and probably something else to be in the headers:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
};
I've tried in multiple ways but I can either send header or form-data, failed to send both.
I've finally managed to do it.
Answer in code snippet below:
var querystring = require('querystring');
var request = require('request');
var form = {
username: 'usr',
password: 'pwd',
opaque: 'opaque',
logintype: '1'
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
request({
headers: {
'Content-Length': contentLength,
'Content-Type': 'application/x-www-form-urlencoded'
},
uri: 'http://myUrl',
body: formData,
method: 'POST'
}, function (err, res, body) {
//it works!
});
This should work.
var url = 'http://<your_url_here>';
var headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
'Content-Type' : 'application/x-www-form-urlencoded'
};
var form = { username: 'user', password: '', opaque: 'someValue', logintype: '1'};
request.post({ url: url, form: form, headers: headers }, function (e, r, body) {
// your callback body
});
I think it's just because you have forgot HTTP METHOD. The default HTTP method of request is GET.
You should add method: 'POST' and your code will work if your backend receive the post method.
var req = require('request');
req.post({
url: 'someUrl',
form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
},
method: 'POST'
},
function (e, r, body) {
console.log(body);
});
I found the solution to this problem and It should work. I'm sure about this because I also faced the same problem
here is my solution----->
var request = require('request');
//set url
var url = 'http://localhost:8088/example';
//set header
var headers = {
'Authorization': 'Your authorization'
};
//set form data
var form = {first_name: first_name, last_name: last_name};
//set request parameter
request.post({headers: headers, url: url, form: form, method: 'POST'}, function (e, r, body) {
var bodyValues = JSON.parse(body);
res.send(bodyValues);
});
Just remember set method to POST in options. Here is my code
var options = {
url: 'http://www.example.com',
method: 'POST', // Don't forget this line
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-MicrosoftAjax': 'Delta=true', // blah, blah, blah...
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
},
form: {
'key-1':'value-1',
'key-2':'value-2',
...
}
};
//console.log('options:', options);
// Create request to get data
request(options, (err, response, body) => {
if (err) {
//console.log(err);
} else {
console.log('body:', body);
}
});