post request node.js

Moti Mashiah
Moti Mashiah used Ask the Experts™
on
Hi guys,
I am trying to check login in a external website and I am using node.js.
the site I am trying to check the login contain form that submit username and password:
Here is the dom of this website:
<form action="/PSAWeb/login" id="LoginForm" method="post" novalidate="novalidate">        <div class="sixteen columns">
            
            <div id="FakeSummary" class="validation-summary-errors">
                <ul id="validation-ul"></ul>
            </div>
        </div>
        <fieldset class="five columns">
            <legend>Log In</legend>
            <ol>
                <li>
                    <label for="Username">Username</label>
                    <input data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="">
                </li>
                <li>
                    <label for="Password">Password</label>
                    <input id="Password" name="Password" required="Required" type="password" class="form-control" autocomplete="off" aria-required="true">
                    <span toggle="#Password" style="float:right; margin-right:5px; margin-top:-30px; position:relative; z-index:2; display:none;" class="togglePassword"><i class="icon-eye-open icon"></i></span>
                </li>
                    <li>
                        <label for="Schema">Database</label>
                        <div id="SchemaChosen">
                            <span style="width:85%;float:left">
                                

<select data-val="true" data-val-number="The field Database must be a number." data-val-required="The Database field is required." id="Schema" name="Schema" style="width:100%;" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true"></select><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-Schema-container"><span class="select2-selection__rendered" id="select2-Schema-container"><span class="select2-selection__placeholder">Select an Item</span></span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
<script>
  $(document).ready(function () {
    $('#Schema').select2({
        placeholder: 'Select an Item'
                      });
  });
</script>
                            </span>
                            <a onclick="executed = false;RunQuery();" href="javascript:void(0)" style="float:left;margin:8px"><i class="icon icon-refresh" id="Refresh"></i></a>
                            <br>
                            <br>
                        </div>

                        <div id="Progressbar" style="display:none;width:100%;"></div>
                    </li>
                <li>
                    <input type="submit" value="Log In"><br><br>
                </li>
                <li>
                    <a href="" onclick="popUpWindow(); return false;">Forgot Password</a>
                </li>
                <li>
                    <a target="_blank" href="https://psarcweb.com/front/ ">Latest Updates</a>
                </li>
            </ol>
        </fieldset>
</form>

Open in new window


Here is what I am trying to do from the node.js side:
const request = require("request");
 var r = request.post('https://psarcweb.com/login', function optionalCallback(err, httpResponse, body) {
 debugger;
 console.log(body)
 console.log(body)
 })
 var form = r.form();
 form.append('Username', 'motimash');
 form.append('password', 'laksdwo');

Open in new window

Issue:
this site is having an issue to login sometimes and I would like to run a script from node.js and check if the return come with 200.
for now I am getting html return.
is there a possibility to hit the website and check the login like fiddler with node.js.?

please, let me know if you have any solution to do this check.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
what is your final goal?
Moti Mashiah.NET Developer

Author

Commented:
My final goal is to run a script from the node.js against this site and and check if the return was 200 or it failed.
leakim971Multitechnician
Top Expert 2014

Commented:
I mean do you need this for an application?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Moti Mashiah.NET Developer

Author

Commented:
Nope, just for testing.
Just want to run it and get the return.
Moti Mashiah.NET Developer

Author

Commented:
I would like to send post to this site with the username and password params and check the return.
Multitechnician
Top Expert 2014
Commented:
you can use PhantomJS for that purpose :
var page = require("webpage").create();
page.open("https://psarcweb.com/login", "post", "Username=motimash&password=aksdwo", function (status) {
    if(status == "success")
        console.log(page.content);
    else
        console.log("Unable to post!");
    phantom.exit();
});

Open in new window


or CasperJS...
Moti Mashiah.NET Developer

Author

Commented:
I am getting this error;
TypeError: require(...).create is not a function
    at Object.<anonymous> (D:\Codes\MyCode\ProjectsJS\nodeApp1\app.js:8:31)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Moti Mashiah.NET Developer

Author

Commented:
var page = require("webpage").create();

page.open("https://psarcweb.com/PSAWeb/Account/Login?ReturnUrl=%2FPSAWeb%2F", "post", "Username=motimash&password=aksdwo", function (status) {
    if(status == "success")
        console.log(page.content);
    else
        console.log("Unable to post!");
    page.phantom.exit();
});

that is how I wrote it as you suggester. I also installed this package webpage.

npm install webpage
leakim971Multitechnician
Top Expert 2014

Commented:
you need to run this with phantomjs not nodejs
Moti Mashiah.NET Developer

Author

Commented:
oh k, I am not familiar with phantomjs. I will take a look now.
Moti Mashiah.NET Developer

Author

Commented:
Sorry, but can you explain what is phantomjs.?
Moti Mashiah.NET Developer

Author

Commented:
I took a look at this phantomjs and it is kinda of node.js. still I don't understand what is the difference.
Please, if you can explain I will appreciate.

I am tired of reading :) .
leakim971Multitechnician
Top Expert 2014

Commented:
you had some reflex using "npm install webpage" and so on
so to do a shortcut : it's a nodejs with some predefined "modules" allowing you to play with webpages just like you can do it with a browser console.

<< Scriptable Headless Browser >>
Moti Mashiah.NET Developer

Author

Commented:
oh k. thank you so much for the explanation.
I ran the script with phantomjs.exe myscript.js and I got html return.

<article>
<form action="/PSAWeb/Account/Login?ReturnUrl=%2FPSAWeb%2F" id="LoginForm" method="post" novalidate="novalidate">        <div class="sixteen columns">
            <div class="validation-summary-errors"><span>Log in was unsuccessful. Please correct the errors and try again.</span>
<ul><li>The username or password entered is not correct. Please try again.</li>
</ul></div>
            <div id="FakeSummary" class="validation-summary-errors">
                <ul id="validation-ul"></ul>
            </div>
        </div>
        <fieldset class="five columns">
            <legend>Log In</legend>
            <ol>
                <li>
                    <label for="Username">Username</label>
                    <input data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="motimash">
                </li>
                <li>
                    <label for="Password">Password</label>
                    <input id="Password" name="Password" required="Required" value="aksdwo" type="password" class="form-control" autocomplete="off" aria-required="true">
                    <span toggle="#Password" style="float:right; margin-right:5px; margin-top:-30px; position:relative; z-index:2; display:none;" class="togglePassword"><i class="icon-eye-open icon"></i></span>
                </li>
                    <li>
                        <label for="Schema">Database</label>
                        <div id="SchemaChosen" style="display: none;">
                            <span style="width:85%;float:left">


<select data-val="true" data-val-number="The field Database must be a number." data-val-required="The Database field is required." id="Schema" name="Schema" style="width:100%;" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true"></select><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-Schema-container"><span class="select2-selection__rendered" id="select2-Schema-container"><span class="select2-selection__placeholder">Select an Item</span></span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
Moti Mashiah.NET Developer

Author

Commented:
I sent you the original URL to test can you take a look. I need return of fail or success.
https://psarcweb.com/PSAWeb/Account/Login?ReturnUrl=%2FPSAWeb%2F
var page = require("webpage").create();

page.open("https://psarcweb.com/PSAWeb/Account/Login?ReturnUrl=%2FPSAWeb%2F", "post", "Username=motimash&password=aksdwo", function (status) {
    if(status == "success")
        console.log(page.content);
    else
        console.log("Unable to post!");
    page.phantom.exit();
});
leakim971Multitechnician
Top Expert 2014

Commented:
console.log(page.content); // this return the page content because of a 200 / status == "success"
console.log("Unable to post!"); // this when it fail

what else?
Moti Mashiah.NET Developer

Author

Commented:
yes, I get this point, but how can I know if user successfully logged in?  this user name and password is fake it suppose to return  unsuccessful login. I guess I just can check if the server responded successfully.
leakim971Multitechnician
Top Expert 2014

Commented:
the "what else?" was not intend to be rude but as you was talking about status code 200, I was not sure what miss?

would like to run a script from node.js and check if the return come with 200.
your next comment prove we achieve our goal :
yes, I get this point

now you want to know if the login is un/successful which is totaly different to get an http status/error code
Moti Mashiah.NET Developer

Author

Commented:
Sorry, I missed interpreted your comment.

If you have any idea how to check the login as well let me know.
Thank you so much.
leakim971Multitechnician
Top Expert 2014

Commented:
starting from here : https://phantomjs.org/page-automation.html

var page = require("webpage").create();
page.open("https://psarcweb.com/login", "post", "Username=motimash&password=aksdwo", function (status) {
    if(status == "success") {

  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    page.evaluate(function() {
      var error = document.querySelector(".validation-summary-errors");
       if(error && error.textContent.indexOf("Log in was unsuccessful")>=0)
              console.log("login fail");
       else
              console.log("login success");
    });
    phantom.exit()
  });


    }
    else
        console.log("Unable to post!");
    phantom.exit();
});

Open in new window

Moti Mashiah.NET Developer

Author

Commented:
thank you for your last post.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial