简体   繁体   English

Node JS和成功进行外部Web调用?

[英]Node JS and making external web calls successfully?

Hi I am trying to start learning NodeJS now and am in the middle of creating an application. 嗨,我正在尝试开始学习NodeJS,并且正在创建应用程序。 The goal currently is to call a website through node, get an authentication token, then call that website again now with a POST payload which includes my login info and the auth token. 当前的目标是通过节点调用网站,获取身份验证令牌,然后使用包含我的登录信息和auth令牌的POST有效负载再次调用该网站。 I have created the same program using python and i get a 200 response where in nodeJS i am getting a 302. 我已经使用python创建了相同的程序,并且收到200响应,而在nodeJS中我得到了302。

I believe thats a quick solution, the main meat of the problem I guess is my lack of understanding in NodeJS where: 1. If I am supposed to nest these requests calls into one another because they are supposed to be a part of the same 'session' and 2. If so how do I go to the last url which is, example.com/poll and be able to store/modify that information (which is just a json) because/if i go to example.com/poll url using a browser, the browser automatically downloads a file which it contains is a JSON format and doesnt just display it, which is what i need. 我认为这是一种快速解决方案,我认为问题的主要根源在于我对NodeJS缺乏理解:1.如果应该将这些请求调用嵌套在一起,因为它们应该属于同一请求,会话”和2。如果是的话,我该如何转到最后一个网址,例如example.com/poll,并能够存储/修改该信息(只是json),因为/如果我转到example.com/poll使用浏览器访问url,浏览器会自动下载一个文件,它包含的是JSON格式,而不仅仅是显示它,这正是我所需要的。 so that i can either save that data in a string or etc. and not download it 这样我就可以将数据保存在字符串等中,而不下载它

In python I do this (Create a session than make the two calls) 在python中,我这样做(创建会话而不是进行两次调用)

url = "https://example.com/"
session = requests.session()
first_req = session.get(url)
auth_token_str = re.search(XXX, first_req.text)
login_url = 'https://example.com/sessions'
payload = { 'session[username_or_email]' : 'username', 'session[password]' : 'password', 'redirect_after_login':'/', 'authenticity_token': authenticity_token }
login_req = session.post(login_url, data=payload, headers=user_agent)
print "login_req response: ", login_req.status_code //gets me 200

then in Node JS: 然后在Node JS中:

var initLoad = {
method: 'GET',
url: 'https://example.com/',
headers: {
    'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  }
};
request(initLoad, function(error, response, body) {
  if (error) throw new Error(error);
  var $ = cheerio.load(body, {xmlMode: false});
  var authenticityToken = $("input[name=authenticity_token]").val();
  console.log(authenticityToken);
  var options = {
    method: 'POST',
    url: 'https://example.com/sessions',
    headers: response.headers,
    form: {
      'session[username_or_email]': 'someUsername',
      'session[password]': 'somePassword',
      redirect_after_login: '/',
      authenticity_token: authenticityToken
    }
  };
  request(options, function(error, response2, body2) {
    if (error) throw new Error(error);
    console.log(response2.statusCode); //gets me 302 not 200
    var analytics_url = 'https://example.com/poll';
    var tripleload = {
      method: 'GET',
      url: analytics_url,
      headers: response2.headers
    };
    request(tripleload, function(error, response3, body3) {
      if (error) throw new Error(error);
      res.end(body3);
    });
  });
});

302 means temporarily moved redirection which you get due error page being displayed to you (or served to your server in this case). 302表示临时移动的重定向,您将得到适当的错误页面,该页面将显示给您(或在这种情况下提供给您的服务器)。 There is something with this call that you are doing wrong, maybe url is wrong if generated like this. 此调用存在某些问题,您做错了,如果这样生成,则url可能是错误的。

Your code is messy due you being newbie in node and due the fact you use request which is barebone and offers little to no comfort in writing this stuff. 由于您是Node的新手,并且您使用的请求是准系统,因此编写代码时几乎没有甚至没有安慰,因此您的代码很乱。

Use something like Axios: https://github.com/mzabriskie/axios to make it easier to write requests like this. 使用类似Axios的东西: https : //github.com/mzabriskie/axios可以更轻松地编写这样的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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