我正在编写一个能够访问API的脚本(不幸的是我无法透露)。 它允许我提交URL,路径,端口和几个布尔字段。
我的脚本首先接收一个URL列表(每个URL在一个新行上)并将它们放在一个数组中。每个网址都有自己的索引。
然后我有一个创建和发布http请求的函数。
以下是请求中发送的内容:
{ "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" }
其中url = array[counter]
从我通过各种console.logs可以看到的内容,实际写入功能的所有内容都有效。
此时,我相信永远不会发送JSON,因为我的回复始终为undefined
或更准确,{"response":{}}
这是我的完整代码:
var http = require('http');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '8080',
path: '/api/scan',
method: 'POST'
};
fs.readFile('urls.txt', function(err, data) {
//if (err) throw err;
//console.log(err);
var array = data.toString().split('\n');
for (var i = 0; i < array.length - 1; i++) {
str = array[i];
array[i] = str.slice(0, -1);
}
//console.log(array);
var emitter = new EventEmitter();
var counter = 1,
n = array.length;
//console.log(n);
// Start with the first request
function PostRequest() {
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
//body = JSON.parse(body);
// Make sure it's working
console.log(body);
// ADD THE CALLBACK
// OR
// TRIGGER EVENT
//PostRequest();
return emitter.emit('ResponseEnded');
});
});
var url = array[counter];
//console.log(url);
var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
console.log(catURL);
var jsonURL = JSON.stringify(catURL);
post_req.write(jsonURL, function(err) {
if (err) throw err;
//console.log(err);
});
post_req.end();
}
emitter.on('ResponseEnded', function() {
counter++;
if (counter < n) {
PostRequest();
} else {
console.log('No more requests');
}
});
PostRequest();
});
我犯了一个愚蠢的新手错误吗?或者这只是提出请求的完全错误的方法?
感谢所有人/任何帮助!
哦,我必须说,我有办法做到这一点,我手动将每个网址键入数组,并且从不使用文件流,并且它的工作方式完美,所以我使用的是错误的错误吗?
以下是硬编码URL数组的代码:
var http = require('http');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '8080',
path: '/api/scan',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var array = ['ssl.com', 'google.com', 'hamzakhan.org'];
var emitter = new EventEmitter();
var counter = 0,
n = array.length;
console.log(n);
function PostRequest() {
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body);
// Make sure it's working
console.log(body.response.subject);
// ADD THE CALLBACK
// OR
// TRIGGER EVENT
//PostRequest();
return emitter.emit('ResponseEnded');
});
});
var url = array[counter];
var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
post_req.write(JSON.stringify(catURL), function(err) {
//console.log(err);
post_req.end();
});
}
emitter.on('ResponseEnded', function() {
++counter;
if (counter < n) {
PostRequest();
} else {
console.log('No more requests');
}
});
// Start with the first request
PostRequest();
答案 0 :(得分:0)
这是我曾经使用过的一个脚本:
var http = require('http');
var postData = querystring.stringify ({data:JSON.stringify ({timestamp:"823738632"})} );
var options = {
hostname: 'www.google.com',
method: 'POST',
port: 80,
path: '/ncr',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var sendRequest = function(options)
{
that = this;
that.req = http.request(options,function(res)
{
// console.log("Request began");
var output = '';
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function () {
console.log(output);
});
});
that.req.on('error', function (err)
{
console.log("ServerError");
console.log('error: ' + err.message);
});
that.req.write(postData);
that.req.end();
};
sendRequest(options);
答案 1 :(得分:0)
好的,所以我在这里所做的一切都是正确的。我唯一的问题是,一小时前我曾想过可能发送标题是可选的......它不是。
我忘记了void create_list(int v, struct list ***array); ... create_list(v, &array);
:
void create_list(int v, struct list ***array){
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct list*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if ((*array)[m] == NULL)
(*array)[m] = tmp;
else {
ptr = (*array)[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}
感谢您的帮助。