Sending URL with Nodejs and Get Response - javascript

Hey guys can someone help me with that issue
im new on node.js and im studing and starting a project.
what i need to do is:
i need to send an url to a webservice and get the response from the webservice, for example:
i send http://www.webservice.com.br/getserver?user=myuser&password=mypassword
after that the webservice will give me a response at his page a succes/error message or a new url for login example http://history.webservice.com.br
so i need to get the response after sending an url someone know how can i do that with node.js ?
in php would be something like that
/*
* $sendurl = "myUrlHere";
* $getserver = file($sendurl);
* var_dump($getserver);
*/
in this php example i send the url and i get the response withou leaving my page.
if someone know how i do that in node or give me a way i will appreciate.
Thank you so much.

First of all, sending username passwords directly in url get requests is a disaster! By the way, I think you are just playing around with node, so this is forgivable ;) Following is a sample code for you to do http requests to your web service:
var http = require('http');
http.get("http://www.webservice.com.br/getserver?user=myuser&password=mypassword", function(res) {
//this is http response status code
console.log(res.statusCode);
//this is headers
console.log(res.headers);
res.setEncoding('utf8');
res.on("data", function(chunk) {
//this is body
console.log(chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

Related

Intercept outgoing HXR requests and their content

I am looking to monitor all outgoing HXR requests on the page in order to intercept a specific one that I wish to extract its data to log.
Here's the specific request I am trying to intercept:
Request header from the console
Request body I want to extract
Here's what I tried so far:
(function(send) {
XMLHttpRequest.prototype.send = function(body) {
var info="send data\r\n"+body;
console.log(info);
send.call(this, body);
};
})(XMLHttpRequest.prototype.send);
This seems to be somehow working because I get some requests logged in the console but not the ones I am looking for.
I also get some send data [object FormData] logged.
I am probably missing something about which HXR requests I am intercepting with my script and would love some help!
Thanks for your time!
This may just be the reason for the implicit conversion, you can try the JSON.Stringify(body) method.

Node.js - How to send sessionID to GET method in Express via request package? [duplicate]

I'm trying to use NodeJS to scrape a website that requires a login by POST.
Then once I'm logged in I can access a separate webpage by GET.
The first problem right now is logging in. I've tried to use request to POST the login information, but the response I get does not appear to be logged in.
exports.getstats = function (req, res) {
request.post({url : requesturl, form: lform}, function(err, response, body) {
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(body);
res.end();
});
};
Here I'm just forwarding the page I get back, but the page I get back still shows the login form, and if I try to access another page it says I'm not logged in.
I think I need to maintain the client side session and cookie data, but I can find no resources to help me understand how to do that.
As a followup I ended up using zombiejs to get the functionality I needed
You need to make a cookie jar and use the same jar for all related requests.
var cookieJar = request.jar();
request.post({url : requesturl, jar: cookieJar, form: lform}, ...
That should in theory allow you to scrape pages with GET as a logged-in user, but only once you get the actual login code working. Based on your description of the response to your login POST, that may not be actually working correctly yet, so the cookie jar won't help until you fix the problems in your login code first.
The request.jar(); didn't work for me. So I am using the headers response to make another request like this:
request.post({
url: 'https://exampleurl.com/login',
form: {"login":"xxxx", "password":"xxxx"}
}, function(error, response, body){
request.get({
url:"https://exampleurl.com/logged",
header: response.headers
},function(error, response, body){
// The full html of the authenticated page
console.log(body);
});
});
Actualy this way is working fine. =D
Request manages cookies between requests if you enable it:
Cookies are disabled by default (else, they would be used in
subsequent requests). To enable cookies, set jar to true (either in
defaults or options).
const request = request.defaults({jar: true})
request('http://www.google.com', function () {
request('http://images.google.com')
});

How to receive a nodejs data response from ajax

I am currently learning express, ajax, and nodejs. So currently I am trying to make ajax and nodejs interact with each other. I am sending a client request, using ajax, to a nodejs server. Currently, I am able to invoke app.get in the server correctly, i.e. it works fine until the call to console.log(req.query); at the server side. However, I am having trouble accepting data, that is sent by the server. The client side code does not alert any returned data as I tried to make it to.
This is my code on the client side.
function login(){
var obj = '{"username":"'+$('#username').val()+'", "password":"'+$('#password').val()+'"}';
$.ajax({
type:'GET',
dataType :'json',
data:{"username": $("#username").val(),
"password": $("#password").val()},
url:'http://localhost:10351/function',
success: function (data) {
alert("ajax callaback response:"+JSON.stringify(data));
}
})
This is my code on the server side.
var express = require('express');
var app = express();
var http = require("http");
var server = http.createServer();
var portNumber = 10351;
app.use(express.static('public'));
app.get('/function', function(req, res) {
console.log(req.query);
res.send(JSON.stringify(req.query));
});
app.listen(portNumber, function() {
console.log('Currently listening on port ' + portNumber);
});
Any tips?
I was looking at the answer in the following link:
Node.js, Ajax sending and receiving Json
and I thought maybe i could use response.end and response.setHeader instead?
I'm not quite sure if my problem lies on the client side or the server side.
You don't
Listen to the right port (10901 vs 10351)
Send any parameters in the query section of the url
Need JSON.stringify because you already get a string in (req.query)
You should:
Make the ports match
Send parameters in the url ex:(url:'http://localhost:10351/function?foo=bar)
Remove the stringify ex:
res.send(req.query)
alert("ajax callaback response:"+data);
response.end is not needed if you already use the res.send method, and response.setHeader would be helpful if you want to mark this content as json, so you can put a Content-Type: application/json header,
Or use res.json to do both of these things for you (stringify the json and put the right header for you)
Your ports don't match in code. You said your call worked until console.log(), if the ports don't match it won't even enter app.get() in server.
I think you just posted a different code for client.
I will suggest you to try postman which is chrome extension that will be really helpful to send and get ajax request responses. Using you code just by changing the port number I am able to get the response in postman.

Cannot get proper json response from my simple nodejs server

SERVER : cloud server, Ubuntu 15.04 x64. nginx is installed listening port 80.
SERVER program : written using NodeJS, simply response in json. like { msg : "done" }
Client (HTML page using React and JQuery) : simple ask the server for the json data.
The problem is on client
// 1)
this.serverRequest = $.get({url:'https://api.github.com/users/octocat/gists', success:function(ret) {
// 2)
//this.serverRequest = $.get({url:'http://my_server_ip_address:7534/test', success:function(ret) {
alert("ok done");
}.bind(this), error:function(e) {
alert("Error : " + e);
}.bind(this)});
I have no problem with 1) request. I do receive some data.
but with 2) request, 'error' function is called and executing 'alert('Error :' + e);' only shows 'Error : [object Object]'. It does not show any useful information..
So I thought my nodejs server program has some problems and I went on searching for answers. I think I try all possible answers..
1) setting iptable to accept port 7534.
2) making nodejs server to listen on 0.0.0.0
3) using res.json().. res.jsonp()
I can see {msg : "done"} if I try with Chrome. so I guess my nodejs server works.
Look the message of error. Instead of alert(e) use console.log(e), for see properties of object e.
ok following #Juven_v 's answer..I figured the 'object' was Error object. And I was able to see the error message through console.log(e).
Here is summary of the solution I found:
1) When the server program responses, put header **Access-Control-Allow-Origin with value *.
I am using nodeJS with express so the code is
res.header('Access-Control-Allow-Origin', '*');
And the client requests with the url.
I use JQuery and the code is
$.get({
url: "http://address!!!!",
type: "GET",
success: function(result) {
console.log("yay");
console.log(result);
},
error: function (err) {
console.log(err);
},
});
If the above client code does not work, then try to add 'dataType: "json"'
2) If I add the following code in the server program..
res.header('Access-Control-Allow-Headers', 'Content-type');
then the client request must have
//contentType: "application/json",
References :
How does Access-Control-Allow-Origin header work?
jQuery AJAX to node server throws net::ERR_CONNECTION_REFUSED
net::ERR_CONNECTION_REFUSED Error jQuery ajax node.js
THANK YOU ALL!!!!

How to send html gmail message through javascript gapi?

I am trying to send email message using JavaScript Gmail API, but I cannot find how to do it.
Well, I have code to send normal email message, but all html is displayed like text.
Here is code:
var email = "From: me\r\nTo: "+to+ "\r\nBcc: " +bcc+ "\r\nSubject: "+subject+"\r\n\r\nbody: " + content;
SEND MESSAGE WITH METADATA ONLY
function sendMessage(email,id)
{
var base64EncodedEmail = btoa(unescape(encodeURIComponent(email))).replace(/\//g,'_').replace(/\+/g,'-');
var request = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': base64EncodedEmail
},
});
request.execute(callbacksend);
}
When I have tried to set content type as html in response, I receieved an error response from server that I need to use upload method.
Indeed, here - https://developers.google.com/gmail/api/v1/reference/users/messages/send
Is said:
Upload URI, for media upload requests:
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send
but while trying to use
gapi.client.load('upload', 'v1', callback);
or just trying to run gapi.client.upload.gmail.users.messages.send
its not found...
I have no idea how to perform such request, or another way to send html message from gmail. Perhaps I need to try it from pure XMLHttprequest, but better to find the way to perform it from gapi.
p.s I've made such request through google apps script:
GmailApp.sendEmail(<recipient>,'subject', <message>,{htmlBody:<message>, bcc:'test#gmail.com'});
But need to perform it with javascript.
Thanks!

Categories

Resources