Search code examples
javascriptnode.jsnode-fetch

get call status in nodejs from grandstream phone


I am trying to fetch the call status of a grandstream phone by a nodejs script. But I've run in some trouble. The first request is going all fine, and returning that I am authenticated. The second request isn't going well, it says that I'm not authenticated.

How do I set the credentials or the cookie from the first request in the second request, so it knows that I'm loggedin?

First request response:

Response=Success
Message=Authentication accepted
Needchange=0
Ver=1.0.3.92

First request response headers:

{
    'status': '200', 
    'content-length': '79', 
    'content-location': 'http://192.168.0.1/manager?action=login&username=XXXXXX&secret=XXXXXX', 
    'set-cookie': 'phonecookie="XXXXXX";HttpOnly, type=admin;, Version="1";, Max-Age=900', 
    'server': 'Enterprise Phone', 
    'pragma': 'no-cache', 
    'cache-control': 'no-cache', 
    'date': 'Wed, 14 Jun 2017 10:22:29 GMT', 
    'content-type': 'text/plain'
}

Second request response:

Response=Error
Message=Authentication Required

App.js Script:

    var fetch = require('node-fetch');

    var host = '192.168.0.1';
    var loginUrl = "/manager?action=login&username=XXXXXX&secret=XXXXXX";
    var statusUrl = "/manager?action=lineStatus&line=0";

    function makeRequest(url)
    {
        fetch("http://" + host + loginUrl).then(function(resultLogin) {
            var resultAuth = result.body();

            fetch("http://" + host + statusUrl, {method: 'GET').then(function(resultStatus) {
                var resultStatus = resultStatus.body();
            });


        });
    }


makeRequest();

Solution

  • Use fetch-cookie to let node-fetch store and send back cookies according to the url.

    var fetch = require('fetch-cookie')(require('node-fetch'))