How to make a request to a website, which has user authentication, to access its contents?

Viewed 40

I am working on an IOS app as my college project. Basically my college has a website which has login form[no sign up forms there], and students using their unique college ID and their own password can login into this website and access things like courses they have registered for, their grades etc. Now in my app i have a log in form and i want the students to be able to login with the same existing ID and password and access the same information as they would be able to do on the website itself. Simply speaking, i want to access the post authentication information on the website in my app. How do i approach this problem?

1 Answers

It depends if your school server allows request from different origins (different domain). If, yes just check when you login what info it posts and to which address. Then send a request to this address with the same info and check the result from the request.

Example: Just use chrome or firefox dev inspector, fill the login form, send it and check the request done.

Let say the request done is a POST @ http://myschool.com/login with username and password form, and the result if successful is a 200 code and a cookie. Then in node js you could do:

const querystring = require('querystring');
const request = require('request');

const form = {
    username: 'usr',
    password: 'pwd',
};

const formData = querystring.stringify(form);
const contentLength = formData.length;

request(
    {
        headers: {
            'Content-Length': contentLength,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        uri: 'http://myschool.com/login',
        body: formData,
        method: 'POST'
    }, 
    function (err, res, body) {
         // check the result here
    }
);
Related