2

This page shows JSON inside plain empty html/body tags. For example:

<html>
  <body>
    **JSON DATA ***
  </body>
</html>

I can access that page fine doing it normally within a browser. But, when I send a request it says that my credentials are invalid (The response error is pretty much in the title). And here is the code

import dotenv = require('dotenv');
dotenv.config({ path: '../logins.env' });
import rp = require('request-promise');
const jsonUrl = `http://availability.emodal.com/ImportAvailability/GetContainerInfoList?sgrdModel=%7B%22searchtext%22:%22%22,%22page%22:1,%22pageSize%22:280,%22sortBy%22:%221%22,%22sortDirection%22:%22asc%22,%22sortColumns%22:%22%22%7D`;

const authOpts = {
  uri: jsonUrl,
  auth: {
    user: process.env.EMODAL_id,
    pass: process.env.EMODAL_pw,
    sendImmediately: false
  },
  method: 'get'
}

rp(authOpts)
.then(resp => {
  console.log(resp);
}).catch(err => { throw Error(err)});

I tried changing everything around. Like adding headers, or putting json: true, or changing sendImmediately to true. Nothing works. What am I not understanding or what am I doing wrong?

Here is an image showing the error in terminal: enter image description here

Thank you

Chris
  • 973
  • 1
  • 8
  • 20
  • 1
    `Here is an image showing the error in terminal` - copy/paste the text would've been *quicker* and easier to work with – Jaromanda X May 17 '17 at 00:08

1 Answers1

0

From my understanding, the issue is: the page of the URL can be displayed successfully after login, but the URL itself cannot be fetched directly by program.

The cause of this issue is: auth option of request-promise (inherited from module request) works by adding Authorization header in HTTP request (document). This is called Basic access authentication and may not be supported by many servers. Unfortunately, in current web world, Basic access authentication is not popular any more (it's not secure), and it is very likely that emodal.com does not support it. Actually, I find a cookie in www.emodal.com named ASP.NET_SessionId which indicates the site is using SessionId cookie to authenticate user.

To fix the problem, I think the program need to login and save the SessionId cookie after login successfully. Then, fetch the URL resource by adding the SessionId cookie in the HTTP request header.

shaochuancs
  • 15,342
  • 3
  • 54
  • 62