How to send HTTP Request in node js under proxy - javascript

Following node js code works well and get all the required results.
But when i run the code in the computer under proxy settings defined. It doesnt give the result.
var options: https.RequestOptions = {
host: item.hostname,
port: +item.port,
path: item.path,
method: 'POST',
headers: newHeader
}
var req = https.request(options, function(res){
if(res.statusCode !== 200){
//reject();
//return;
}
var result = '';
res.setEncoding('utf8');
res.on('data', function(chunk){
result += chunk;
});
res.on('end', function(){
resolve(result);
});
res.on('error', function(e){
reject(e);
});
});
How to enable proxy support in http module requests in NODE JS that it checks if there is any proxy defined and perform that enabled request.
options data in debug mode :
headers:Object
Accept:"application/json;api-version=3.0-preview.1"
Content-Length:104
Content-Type:"application/json"
host:"marketplace.visualstudio.com"
method:"POST"
path:"/_apis/public/gallery/extensionquery"
port:0

The proxy should be the host/port and the path is the final path of the request that the proxy should make (host:port/path).
Basically e.g. if making a request through a proxy on localhost:8888:
path = host + path, host = 'localhost', port = 8888;

Related

How to set https proxy

How on earth do I use an https proxy with http2 in nodejs. I've tried looking around and I can't find any info on it. Take for example the following code:
const client = http2.connect('https://twitter.com');
const req = client.request({':method': 'GET', ':path': '/', ':authority': 'twitter.com', ':scheme': 'https', 'Cookie': loadCookies(cookiejar) });
req.setEncoding("utf8");
var body = "";
req.on('response', (responseHeaders) => {
storeCookies(responseHeaders, cookiejar);
});
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () =>{
client.destroy();
});
So how would I go about routing this request through a proxy, say for example Charles where the proxy would be set 127.0.0.1:8888.

Node.js write http request response

I'm using this code to make an HTTP request to my own server. I'm getting the appropriate response in chunk.
http.createServer(function (req, res) {
var options = {
host: '<my ip>',
port: 8080,
method: 'GET',
path: '/content?data='+somedata
};
var call = http.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
console.log("got response"+chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
call.end();
}).listen(3000);
My question is how I can print this chunk to my browser?
Change one of your two res variables to have a different name. At the moment the response to your request is masking the response you are trying to make.
Then:
response.write(chunk);
See also: https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback
res.write(chunk);
also don't forget to end first call, so the browser will know the request has ended.
res.end("success");

nodejs http post request throws TypeError

I am trying to make a simple server that use google oauth (without express and passportjs, as I want to study the data exchanged).
When my program attempts to send a post request to google, nodejs throws:
http.js:593 throw new TypeError('first argument must be a string or Buffer');
I have checked and make sure that all parameters in query and option are all string, but the error still persist. What could I have missed here?
Here is my code:
// Load the http module to create an http server.
var http = require('http');
var url = require('url');
var fs = require('fs');
var querystring = require('querystring');
var content;
fs.readFile('./test.html',function(err,data){
content = data;
});
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
var path = url.parse(request.url).pathname;
var query = querystring.parse(url.parse(request.url).query);
var code;
if (query!=null) {
code = query.code;
};
if ('/auth/google/callback'==path){
var data = querystring.stringify({
'code': ''+code,
'client_id': 'id',
'client_secret': 'secret',
'redirect_uri': 'http://localhost:8999/auth/google/code/callback',
'grant_type': 'authorization_code'
});
var options = {
hostname: 'accounts.google.com',
port:'80',
path: '/o/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': ''+data.length
}
};
debugger;
var post = http.request(options, function(res){
response.write(res);
response.end();
});
debugger;
post.write(data);
debugger;
post.end();
}
else if (path=='/auth/google/code/callback'){
console.log(request.headers);
console.log(request.url);
}
else response.end(content);
console.log(request.headers);
console.log(request.url);
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8999);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
Many thanks,
I think problem is when you are saying
response.write(res); //it needs a string
I think res is an object here.
try
response.write(JSON.stringify(res));
When you write response or request. It should contain string so you need to change it to
response.write(querystring.stringify(res));
or
response.write(JSON.stringify(res));

Add parameters to HTTP POST request in Node.JS

I've known the way to send a simple HTTP request using Node.js as the following:
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/foo.html'
};
http.get(options, function(resp){
resp.on('data', function(chunk){
//do something with chunk
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
I want to know how to embed parameters in the body of POST request and how to capture them from the receiver module.
Would you mind using the request library. Sending a post request becomes as simple as
var options = {
url: 'https://someurl.com',
'method': 'POST',
'body': {"key":"val"}
};
request(options,function(error,response,body){
//do what you want with this callback functon
});
The request library also has a shortcut for post in request.post method in which you pass the url to make a post request to along with the data to send to that url.
Edit based on comment
To "capture" a post request it would be best if you used some kind of framework. Since express is the most popular one I will give an example of express. In case you are not familiar with express I suggest reading a getting started guide by the author himself.
All you need to do is create a post route and the callback function will contain the data that is posted to that url
app.post('/name-of-route',function(req,res){
console.log(req.body);
//req.body contains the post data that you posted to the url
});
If you want to use the native http module, parameters can be included in body this way:
var http = require('follow-redirects').http;
var fs = require('fs');
var options = {
'method': 'POST',
'hostname': 'example.com',
'path': '/foo.html',
'headers': {
},
'maxRedirects': 20
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
res.on("error", function (error) {
console.error(error);
});
});
var postData = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"examplekey\"\r\n\r\nexamplevalue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--";
req.setHeader('content-type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
req.write(postData);
req.end();

Node.js Requests returning 301 redirects

I'm brand new to node.js, but I wanted to play around with some basic code and make a few requests. At the moment, I'm playing around with the OCW search (http://www.ocwsearch.com/), and I'm trying to make a few basic requests using their sample search request:
However, no matter what request I try to make (even if I just query google.com), it's returning me
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>
I'm not too sure what's going on. I've looked up nginx, but most questions asked about it seemed to be asked by people who were setting up their own servers. I've tried using an https request instead, but that returns an error 'ENOTFOUND'.
My code below:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
var options = {
host:'ocwsearch.com',
path:
'/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',
method: 'GET'
}
var req = http.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
Sorry if this is a really simple question, and thanks for any help you can give!
For me the website I was trying to GET was redirecting me to the secure protocol. So I changed
require('http');
to
require('https');
The problem is that Node.JS's HTTP Request module isn't following the redirect you are given.
See this question for more: How do you follow an HTTP Redirect in Node.js?
Basically, you can either look through the headers and handle the redirect yourself, or use one of the handful of modules for this. I've used the "request" library, and have had good luck with it myself. https://github.com/mikeal/request
var http = require('http');
var find_link = function(link, callback){
var root ='';
var f = function(link){
http.get(link, function(res) {
if (res.statusCode == 301) {
f(res.headers.location);
} else {
callback(link);
}
});
}
f(link, function(t){i(t,'*')});
}
find_link('http://somelink.com/mJLsASAK',function(link){
console.log(link);
});
function i(data){
console.log( require('util').inspect(data,{depth:null,colors:true}) )
}
This question is old now, but I got the same 301 error and these answers didn't actually help me to solve the problem.
I wrote the same code:
var options = {
hostname: 'google.com',
port: 80,
path: '/',
method: 'GET',
headers: {
'Content-Type': 'text/plain',
}
};
var http = require('http');
var req = http.request(options, function(res) {
console.log('STATUS:',res.statusCode);
console.log('HEADERS: ', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
});
});
req.on('error', function(e) {
console.log('problem with request: ', e.message);
});
console.log(req);
req.end();
so after some time I realized that there's a really tiny mistake in this code which is hostname part:
var options = {
hostname: 'google.com',
...
you have to add "www." before your URL to get html content, otherwise there would be 301 error.
var options = {
hostname: 'www.google.com',

Categories