Tag: 请求

如何解码nocklogging的响应

如何特别解码/解压诺克logging器产生的输出,所以我们可以看到作为文本的响应? 我想我们不明白,如果回应是gzipped和/或编码 当我们把它加载到nock中时,这个对象就能find,我们的testing正如我们所期望的那样运行。 要查看API生成的内容,我们必须将日志logging置于实现文件中。 我们正在录制和保存JSON的回复: nock.recorder.rec({output_objects: true, dont_print: true}); JSON.stringify(nock.recorder.play()) 我们的文件如下所示: [ { "scope": "https://some.api.com:443", "method": "POST", "path": "/auth?key=some_key", "body": { "logonId": "user@api.com", "logonPassword": "secret" }, "status": 400, "response": [ "1f8b0800000000000000458cbd6ac34010067b3fc5c735691263bb741344ec42f827420a492916692d1d9cb461f71c218cdf3d97266e6786b92d00c7aaa205290d1c59cd6d71bb3fff8b376939a1cd6abd7ac003cf89b97a5f96757efecc8ef9aede9fb2fc586455f5f55eeedca33db119757f0f5704266334a2ca4d44ec19170941263f76f06657b62dd6cb2af919ec9357cc7255f0cb403e4014df643689b6687d3b3e450c149b1e534f1113a3a71f868cb8f8c04b7ca48b8fa08efcf8ea16f75fa1776d91ee000000" ], "headers": { "cache-control": "no-store, no-cache, must-revalidate", "content-encoding": "gzip", "content-type": "application/json", "transfer-encoding": "chunked", "connection": "Close" } } ]

ECONNREFUSED nodeJS与docker集装箱内的快递

我正在构build一个在Docker容器中运行的nodejs应用程序。 这是我用来运行容器的命令… sudo docker run -it –rm -p 3000:6001 –name xxx-running xxx 执行上面的命令行,得到如下输出 Running on Locally AppEnv { isLocal: true, app: {}, services: {}, name: 'xxx', port: 6001, bind: '0.0.0.0', urls: [ 'http://localhost:6001' ], url: 'http://localhost:6001' } App started on port http://localhost:6001 由于该应用程序将调用第三方API,所以请求模块是必需的。 每次请求被调用时,我得到以下错误… { [Error: connect ECONNREFUSED 192.155.253.83:443] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', […]

node.js请求包含asynchronous脚本的网页

我正在使用非常简单的请求模块下载网页。 我的问题是,我试图下载的页面有一些asynchronous脚本(具有asynchronous属性),他们不下载与HTTP请求的HTML文档返回。 我的问题是如何使用/不带(最好带)请求模块发出http请求,并且由于某些边缘情况使得整个页面没有例外地下载,如上所述。

createWriteStream vs writeFile?

这两个操作之间的基本区别是什么? someReadStream.pipe(fs.createWriteStream('foo.png')); VS someReadStream.on('data', function(chunk) { blob += chunk } ); someReadStream.on('end', function() { fs.writeFile('foo.png', blob) }); 当使用请求库进行抓取时,我只能使用前一种方法保存图片(png,bmp)等,而后者则有相同的gibbersh(二进制)数据,但图像不会呈现。 他们有什么不同?

以CSV格式获取Google云端硬盘电子表格的内容

我试图通过node.js获取Google Drive Spreadsheet的文件内容。 我正在使用google-api-nodejs-client获取access_token和node-google-drive模块来获取file_id 。 "https://docs.google.com/feeds/download/spreadsheets/Export?key="+file_id+"&exportFormat=csv&gid=0" 当我loginGoogle云端硬盘并转到上面的url时,它会以CSV格式下载电子表格。 当我尝试在隐身模式下转到该url时,它显示了一个login页面,这是我在node.js中获得的请求正文中的同一页面。 request({ "method":"get", "url": "https://docs.google.com/feeds/download/spreadsheets/Export", "qs":{ "key": key, "exportFormat": "csv", "gid": 0 }, "headers":{ "Authorization": "Bearer " + token } }, function(err, response, body){ console.log(response); }); 我试图发送Authorization标题,但我仍然得到一个HTML响应。 包含Sorry, the file you have requested does not exist.

请求总是返回空错误对象

我使用Request和标准的Node.js HTTP模块来编写一个简单的rest服务。 现在,服务器端请求处理程序如下所示: response.writeHead("400","Nope"); response.end(); 客户端代码如下所示: request.get(href,handleResponse); function handleResponse(error,response,body) { console.log(arguments); } response.statusCode是400 ,但error始终为null 。 我需要做什么服务器端获取请求模块来识别响应是一个错误?

当我在一个函数中使用request-promise并返回一个值时,它说undefined

所以从查看请求承诺文档,这里是我所拥有的 function get_data_id(searchValue) { rp('http://example.com?data=searchValue') .then(function(response) { return JSON.parse(response).id; }); } 然后我在脚本的其他地方使用这个代码 console.log(get_data_id(searchValue)); 但是它返回undefined 。 如果我更改return JSON.parse(response).id到console.log(JSON.parse(response).id)我得到以下 undefined valueofID 所以我试图返回的值肯定是有效/正确的,但我不知道如何将它作为一个值返回。

使用asynchronous和请求模块限制请求

我将asynchronous和请求模块组合在一起,以asynchronous方式和速率限制来发出api请求。 这是我的代码 var requestApi = function(data){ request(data.url, function (error, response, body) { console.log(body); }); }; async.forEachLimit(data, 5, requestApi, function(err){ // do some error handling. }); 数据包含我要求的所有url。 我使用forEachLimit方法将并发请求的数量限制为5。 这段代码会使第5个请求停止。 在asynchronous文档中,它说:“迭代器传递一个callback函数,一旦完成,就必须调用它”。 但是我不明白,我该怎么做来表示请求已经完成?

Cookie不会使用request.js发送

所以我有一个API启动,但是我决定从使用nano切换到只使用请求 。 但是由于某种原因,我似乎无法得到cookie现在发送回服务器。 这里是我testing的代码的一个例子。 var request = require('request') var cookieJar = request.jar(); var myCookie; request({ method: 'POST', uri: 'http://127.0.0.1:5984/_session', form: { name: 'test', password: 'test123' } }, function(err, res, body) { if (err) { console.log(err) }; console.log(res.statusCode); console.log(res.headers); console.log(body); myCookie = request.cookie(res.headers['set-cookie'][0]); cookieJar.setCookie(myCookie, 'http://127.0.0.1:5984/_session'); }); request({ method: 'GET', uri: 'http://127.0.0.1:5984/db', jar: cookieJar }, function(err, res, […]

Node.js – 需要睡眠

考虑以下情况: 在我的一个cron作业里,我请求别人的服务,只允许请求3600秒。 该API类似于GetPersonForName=string 。 考虑到我的数据库中有一些people ,我需要尽可能地更新他们的信息,我扫描我的数据库所有的人,并调用这个API。 例 // mongodb-in-use People.find({}, function(error, people){ people.forEach(function(person){ var uri = "http://example.com/GetPersonForName=" + person.name request({ uri : uri }, function(error, response, body){ // do some processing here sleep(3600) // need to sleep after every request }) }) }) 不知道睡觉是否是一个想法,但是我需要等待3600秒。