【问题标题】:PhantomJS Send post data to RESTful APIs and manipulate JSONPhantomJS 将发布数据发送到 RESTful API 并操作 JSON
【发布时间】:2016-02-10 19:21:45
【问题描述】:

使用 PhantomJS 2.1.1 我想对用户进行身份验证并获取受保护数据的列表;到目前为止,我的这种方法没有成功。这里的任何人都可以看看并指出我正确的方向。谢谢。 我想做的是从后端获取 JSON 并在此处使用 Phantom 对其进行测试,作为我构建过程的一部分。

use strict
var AUTH_ENDPOINT = 'http://192.168.1.251';
var SERV_ENDPOINT = 'http://192.168.1.250';
var returnedData = '';

var page = require('webpage').create(),
    server = AUTH_ENDPOINT + '/services/users/login',
    settings = {
        operation: "POST",
        headers: {
           "Content-Type": "application/json"
        },
        data: JSON.stringify({'user':'myusername', 'pword':'123456789'})
    };

page.onResourceRequested = function(requestData, networkRequest) {
    console.log('Request (#' + requestData.id + '): ');
    console.log(JSON.stringify(requestData));
};

page.onResourceReceived = function(response) {
    console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};

page.onLoadFinished = function(status) {
  console.log('Status: ' + status);
  // Do other things here...
};
page.onResourceError = function(resourceError) {
  console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
  console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

page.open(server, settings, function(status) {
  console.log('Status: ' + status);
  // when successful the response from server is a JSON
  // like so '{"authtoken":"23423","user":"myname"}'
  returnedData = JSON.parse(page.plainText);
  // this should be my token
  console.log(returnedData.authtoken);
});

// now use this token on this url
var server = SERV_ENDPOINT + '/services/get/list?data=cars';
var settings = {
    operation: "GET",
    encoding: "utf8",
    headers: {
       "token": returnedData.authtoken
    }
};

page.open(server, settings, function(status) {
    console.log(status);
    if (status !== 'success') {
        console.log('Unable to get!');
    } else {
        var jsonSource = page.plainText;
        var resultObject = JSON.parse(jsonSource);
        // all of the user cars here!!
        console.log(resultObject);
    }
    // then finally close
    phantom.exit();
});

所以这就是我所拥有的,它不起作用输出在这里

Request #1:
{"headers":
[
  {
    "name":"Accept",
    "value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  },
  {
    "name":"Origin",
    "value":"null"
  },
  {
    "name":"User-Agent",
    "value":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
  },
  {
    "name":"Content-Type",
    "value":"application/json"
  },
  {
    "name":"Content-Length",
    "value":"54"
  }
],
"id":1,
"method":"POST",
"postData":"{\"user\":\"myusername\",\"pword\":\"123456789\"}",
"time":"2016-02-10T18:32:36.007Z",
"url":"http://192.168.1.251/services/users/login"}
Unable to load resource (#1URL:http://192.168.1.251/services/users/login)
Error code: 5. Description: Operation canceled 
Response (#1, stage "end"): {"contentType":null,"headers":[],"id":1,"redirectURL":null,"stage":"end","status":null,"statusText":null,"time":"2016-02-10T18:32:36.008Z","url":"http://192.168.1.251/services/users/login"}

【问题讨论】:

    标签: json phantomjs restful-url


    【解决方案1】:

    一个错误可能是“page.plainTex”而不是“page.plainText”,这使代码对我有用。

    【讨论】:

    • 啊,是的,我看错了。它对我有用。您能否使用 curl 发布到网站并取回正确的数据?
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2021-12-16
    • 2017-07-22
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多