Tag: 请求

无法在NodeJS中下载页面

我想在NodeJS中下载页面( https://www.csfd.cz/tvurce/65871 ),但是我只能得到随机数据。 }Ms F +i" ) Jْ; e 7 KM0 LƩ ] Yg b Ow7U J # K 9 L 我以为这只是错误的编码,但即使大小是错误的(下载页面有44K,而这个文件只有19K。更令人惊讶的是,简单的下载python的工程很好。 Python代码: import requests url = "https://www.csfd.cz/tvurce/65871" r = requests.get(url) with open('pyth.txt','wb') as handle: handle.write(r.content) JavaScript代码: const request = require('request-promise') const fs = require('fs') request('https://www.csfd.cz/tvurce/65871').then((html) => { fs.writeFileSync('output.html', html) }) 我也尝试了像request.get等附加方法等,但仍然是相同的结果。 你能告诉我我做错了什么吗?

请求默认值,使用一个函数作为默认头

在使用NPM请求模块的request.defaults API时,是否有人知道我可以将一个函数作为请求头传递,以便每次发出请求时函数的结果都会变成标头值? 即 request.defaults({ headers:{datestamp:() => new Date() })

节点请求如何处理会话

我使用node.JS和请求模块。 我的问题是,我需要对每个请求进行身份validation,因为会话在.then((response) => {})块之外被销毁。 如何将创build的会话保存在一个类中供以后使用? 我尝试了一切都没有成功。 这是一个不工作的代码片段 login() { const getLoginUrl = 'https://www.demourl.com/' const postLoginUrl = 'https://www.demourl.com/account/login/' rp({ url: getLoginUrl, jar: this.cookieJar, method: 'GET' }) .then((body) => { var csrftoken = this.cookieJar.getCookies(getLoginUrl)[1].toString().split('=')[1].split(';')[0]; var args = { url: postLoginUrl, json: true, method: 'POST', data: { username: this.username, password: this.password }, headers: { 'method': 'POST', 'path': '/account/login/', […]

如何正确处理Promise.all:获取undefined

我试图从一些对不同的REST API的请求中得到一个数组充满了信息。 我想使用Promise.all来做到这一点,但由于某种原因,它产生了一堆内部undefined的数组。 [undefined,undefined,undefined,undefined] 这是我的代码: var _ = require("lodash");//Used exclusively to check if the result from the request is an object var ccxt = require("ccxt");//External library used to make the requests let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"]; //Array on which the Promise.all is based function test(p) { for (var i = 0; i < […]

Http请求上的Node.jsparsing错误

我有一个非常简单的请求,但警告我Parse Error : var http = require('http'); var url = require('url'); var opts = { host: 'www.appletreebooks.com', path: 'www.appletreebooks.com/appIndex.php?c=eshop&m=get_hot_new_book_list&per_page=1&devid=c4c8874d16d84cde8fc7b9037ad8e26465bd1560&uid=1&ln=sn', } var req = http.request(opts, function (res) { res.setEncoding('utf8'); var data = "" res.on('data', function(d) { data += d; }) res.on('end', function() { console.log(data); }) }) req.on('error', function(e) { console.log('problem with request: ' + e.message); }); […]

如何从服务器端的多个input字段(Express.js / Node.js)中查找数据?

我有一个具有多个描述文本字段的表单,文件select字段并提交跨域请求(CORS变体)button: <form class="upload" id="upload_form" enctype="multipart/form-data" method="post" action="http://localhost:3001/upload/1234567890"> <div class="row-fluid"> <div class="span5 row-fluid" id="description" style="margin-left:0px;"> <div> <label>Title</label> <input class="span12" type="text" placeholder="Title" id="description_title" name="description_title"/> <label>Author</label> <input class="span12" type="text" placeholder="Author" id="description_author" name="description_author"/> <label>Tags</label> <input class="span12" type="text" placeholder="Tags" id="description_tags" name="description_tags"/> <label>Description</label> <textarea class="span12" id="description_textarea" name="description_textarea" rows="5" style="resize:none"></textarea> <div id="buttons" class="row-fluid" style="margin-top: 5px"> <div class="span12"> <span class="span5 btn btn-primary btn-file" […]

如何使用node.js中的参数获取HTTP请求?

我正在使用NODE.js使用'请求'模块Im: 有用: var request = require('request'); request('http://www.site.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); } }); 当我尝试包括参数崩溃! var request = require('request'); request('http://www.site.com/index.php?action=y9sadf98yh', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); } }); Error: undefined is not a valid uri or options object. at request […]

pipe道图像与在请求中发送node.js中的callback主体

我正在使用node.js 0.10.33和请求2.51.0。 在下面的例子中,我构build了一个简单的Web服务器,它使用请求代理图像。 有两条路线设置代理相同的图像.. / pipe只需将原始请求传递给响应 / callback等待请求callback,并将响应头和主体发送给响应。 pipe道示例按预期方式工作,但callback路由不会呈现图像。 标题和主体看起来是一样的。 callback路线如何导致图像中断? 以下是示例代码: var http = require('http'); var request = require('request'); var imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T'; var server = http.createServer(function(req, res) { if(req.url === '/pipe') { // normal pipe works request.get(imgUrl).pipe(res); } else if(req.url === '/callback') { // callback example doesn't request.get(imgUrl, function(err, resp, body) { if(err) […]

nodejs应用程序如何请求我的另一个https服务器?

下面是nodejs应用程序来请求我的另一个https服务器 var https = require('https'); jsonObject = JSON.stringify({"arg1":"4","arg2":"True"}); // prepare the header var postheaders = { 'Content-Type' : 'application/json', 'Content-Length' : Buffer.byteLength(jsonObject, 'utf8') }; // the post options var optionspost = { host : 'https://www.example.com/', path : '/my/path/?arg1=4&arg2=True', method : 'POST', headers : postheaders }; var reqPost = https.request(optionspost, function(res) { console.log("statusCode: ", res.statusCode); res.on('data', […]

从节点(服务器到服务器)Node / Express使用请求中获取来自Restful API的数据

我试图连接到位于这里的TradeGecko API :(他们所有的例子都是在ruby – 我使用节点) http://developer.tradegecko.com/ 我有一个特权访问令牌,我创build了FAKE TOKEN:146e29b59e7e8861f462101f610f58168ca5edf2f307e5f7adc3314d24ee8015 如何获得所有产品的GET请求,例如: http : //developer.tradegecko.com/?ruby#list- all-products 非常感谢帮助! 假设我有这样的代码: var express = require('express'); var request = require('request'); var app = express(); var port = process.env.PORT || 3000; var testRouter = express.Router(); myRouter.route('/testRoute') .get(function(req, res){ request('API CALL HERE??', function (error, response, body){ if(!error && response.statusCode == 200){ res.json(body); } }) […]