Tag: httprequest

Node.JS http get请求返回undefined

这应该是非常简单的,但没有获取数据,我得到了未定义。 我在代理服务器后面,而且我不擅长从代理服务器发出请求。 我发现只有一个教程,它解释了当使用代理服务器而不使用外部模块时的http请求,并且没有正确解释。 const http = require('http'); let options = { host: '10.7.0.1', port:8080, path:'www.google.co.uk' } http.get(options,(res) => { let str = ''; res.setEncoding('utf8'); res.on('data', (chunk) => { str += chunk; }) res.on('end', (str) => { console.log(str); }) }) 10.7.0.1是代理服务器的地址,8080是端口。 这段代码的输出是未定义的。 我甚至不知道这个方法是否正确,我无法弄清楚。我读了node http docs,并且尽我所能写了选项对象,如果我错了,就纠正我。 一般来说,如何在使用代理服务器时发出请求(不使用任何外部模块)。

cURL的formsaxios或ajax是什么?

我正在创build一个任务来发送PUT请求,并在节点中使用axios上传.pot文件。 我可以跑 curl -i -u api:<api-key> -F file=@dist/file.pot https://api-link.com/v2/api 它会正常工作 我试过在节点上做这个 var fd = require('form-data'); var axios = require('axios'); var form = new FormData(); form.append('file', 'dist/file.pot'); var header = { headers: Object.assign({}, apiToken(), form.getHeaders()) }; axios.put('https://api-link.com/v2/api/', form, header) .then(function(res) { console.log(res); }) .catch(function(err) { console.log(err); }) 这不像curl命令。 apiToken()是处理curl命令的-u api:<api-key>部分的函数,它返回{'Authorization': 'Basic <api-key>'} 。 我已经检查过该function,因为我能够成功地执行获取请求。 那么在ajax或axios中相当于-F file=@dist/file.pot是什么?

如何获得两个date之间的历史股票数据?

我试图从谷歌金融获得Apple历史数据。 但是开始和结束时间总是一样的。 在谷歌金融模块发送获取请求与这些参数http://www.google.com/finance/historical应导致date之间的历史数据,但它返回去年,而不是。 var params = { q: 'AAPL', startdate: '2014-01-01', enddate: '2014-12-31', output: 'csv' }; google-finance模块的例子: gFinance.historical({ symbol: 'NASDAQ:AAPL', from: '2014-01-01', to: '2014-12-31' }, function (err, quotes) { console.log(quotes); }); 结果总是从今天(2017年8月8日)到一年前(2016年8月8日): … { date: 2017-02-22T20:30:00.000Z, open: 137.38, high: 137.48, low: 136.3, close: 136.53, volume: 20788186, symbol: 'NASDAQ:AAPL' }, { date: 2017-02-23T20:30:00.000Z, open: 135.91, high: […]

Node.js xml2jsparsing器未定义

所有, 我想通过xml2jsparsing一个HTTP响应XML,并运行到一个错误。 我试图按照这些说明: https : //programmerblog.net/parse-xml-using-nodejs/ 我已经安装了模块,似乎正常运行。 我得到的错误。 parser.parseString(soapreplyx, function (err, result) { ^ ReferenceError: parser is not defined 我的应用程序代码如下所示。 // APP – INCLUDE const express = require('express') const path = require("path") const bodyParser = require("body-parser") const hbs = require('hbs') var xml2js = require('xml2js'); var parser = new xml2js.Parser(); // APP – DEFINITION const app […]

在下载XML Feed时处理多种编码scheme

我正尝试通过以下url阅读Feed: http://www.chinanews.com/rss/scroll-news.xml 使用请求模块 。 但我得到的东西有 ʷ ) ( й ) ޹ 。 在回顾XML我发现编码被设置为<?xml version="1.0" encoding="gb2312"?> 但试图设置编码为gb2312 ,我得到未知的编码错误。 request({ url: "http://www.chinanews.com/rss/scroll-news.xml", method: "GET", headers: { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Host": "www.chinanews.com", "Accept-Language": "en-GB,en-US;q=0.8,en;q=0.6" }, "gzip": true, "encoding": "utf8" }, (err, resp, data) => { console.log(data); }); 有没有一种方法,我可以得到的数据不考虑它的编码? 我应该如何处理这个?

如何在nodejs中将http请求发送到未链接到DNS但在hosts文件中注册的url?

我正在使用IP 190.34.24.56远程沙箱(不是真正的IP,只是例如) 主机文件包含这个string: 190.34.24.56 web.sandbox.com 我可以从浏览器访问urlweb.sandbox.com ,但是当尝试发送http请求时,我得到一个错误: ENOTFOUND (未find域名)。 如何将http请求从nodejs发送到没有dnslogging的url? (我不能进入沙箱使用190.34.24.56,因为有主机 – 服务器的工作,确定传入的url和redirect请求到其他端口,所以我只能使用web.sandbox.com )

如何在node.js中正确实现读取stream的callback逻辑?

我正在尝试在node.js中实现小型的RSS阅读器(将呈现feed数据作为html页面)。 我到目前为止已经到了这个地步: var FeedParser = require(__dirname + '/../node_modules/feedparser') , request = require(__dirname + '/../node_modules/request'); exports.news = function(req, res){ var news = []; request('http://feeds.feedburner.com/niebezpiecznik/') .pipe(new FeedParser()) .on('error', function(error) { // … }) .on('meta', function (meta) { console.log('===== %s =====', meta.title); console.log('**** %s ****', meta.description); console.log(); }) .on('readable', function() { var stream = this, item; var i […]

等待请求完成

我目前正在尝试为MediaWiki扩展创build一个node.js包装,并且我需要能够login。目前为止所有的东西似乎都在工作,但是我无法让脚本在返回之前停止并等待请求完成。 这是至关重要的,因为请求必须在返回之前完成,以便它返回适当的值而不是未定义的。 我正在使用node.js的请求模块。 当前的HTTP POST处理程序如下 var post = function(data) { var r; var options = {}; options.url = module.exports.apipath; options.method = "POST"; options.form = data; request(options, function(error, response, body) { if (!error) { if(response.statusCode == 200) { try { if((typeof body) == "string") { var result = JSON.parse(body); r = result; } else { r […]

如何在nodejs中为http.request设置自定义超时?

我有一个http请求,有时收到没有回应(超时err0r)。 我应该如何处理这个问题,可能会减less原来的超时时间,然后重复请求几次。

如何使用包含选项的request.js进行POST请求

我确定这是一个基本的语法问题,但是我试图创build一个需要“请求”结构的API : {"customer":{"firstName":"Regular","lastName":"Joe"}…} 我也想包括一些选项,包括查询参数,即: options = { method: "POST" url: "https://api.rezdy.com/latest/bookings" qs: { apiKey: apiKey, } json: true } 我如何在options散列中包含以前的数据,以便我可以这样调用它? request(options, (err, response, body)-> … ) 我试图用formData这样做: options = { method: "POST" url: "https://api.rezdy.com/latest/bookings" qs: { apiKey: apiKey, } formData: data json: true } 但是我仍然从API获取错误,提示它没有收到数据。 在选项散列中包含数据的正确方法是什么?