简体   繁体   中英

Node.js: Sending form requests on websites

I am trying to log in on this page: http://stomfak.ukim.edu.mk/login_student.php using Node.js. I'm using the request external module for this, but can't get it to work as intended. Here's what I've tried so far:

var request = require('request');

request.post(
    'http://stomfak.ukim.edu.mk/login_student.php',
    { form: {
        korime: 'myEmail',
        lozinka: 'myPassword'
    } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

No log from the above.

var request = require('request');

request.post(
    'http://stomfak.ukim.edu.mk/login_student.php',
    { form: {
        korime: 'myEmail',
        lozinka: 'myPassword',
        Submit: 'Најава'
    } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

I tried the above, because I saw that the post request accepts the Submit parameter too, which is always the value of "Најава". No log from the above either.

var request = require('request');

request.post(
    'http://stomfak.ukim.edu.mk/login_student.php',
    { json: {
        korime: 'myEmail',
        lozinka: 'myPassword'
    } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

This only logs the body of the page that contains the form, even when I send the correct credentials.

Sorry about the language of the website, but I hope that won't be an issue.

Since I'm not sure what the website requires, I would recommend you to:

  • log in the website with inspect mode

  • look at the requested header to add more info to your current header.

  • Then use Postman to send different requests, and see which one works for you

In postman, there's also a Code link that can generate code in different languages, in your case is nodejs. Let me know if that works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM