我正在尝试通过Node.js下载文件,但是我继续找回HTTP400。这是我使用的版本:
OS: Ubuntu 16.04.5 LTS
Node: v8.11.3
我遵循here所示的解决方案。另外,我在公司代理后面,所以我设置:
npm config set proxy proxyhost:proxyip
npm config set http-proxy proxyhost:proxyip
npm set strict-ssl false
我的代码是这样的(download-jdt-ls.js
):
const fs = require('fs');
const http = require('http');
let fileUri = "http://mirror.switch.ch/eclipse/che/che-ls-jdt/snapshots/che-jdt-language-server-latest.tar.gz";
let filePath = "/home/myuser/myfolder/download/che-jdt-language-server-latest.tar.gz";
console.log("fileUri: " + fileUri);
console.log("filePath: " + filePath);
let fileStream = fs.createWriteStream(filePath);
let request = http.get(fileUri, response => {
console.log("statusCode: " + response.statusCode);
response.pipe(fileStream);
console.log("statusCode: " + response.statusCode);
});
console.log("done");
使用node ./download-jdt-ls.js
运行时,得到以下输出:
fileUri: http://mirror.switch.ch/eclipse/che/che-ls-jdt/snapshots/che-jdt-language-server-latest.tar.gz
filePath: /home/myuser/myfolder/download/che-jdt-language-server-latest.tar.gz
done
statusCode: 400
statusCode: 400
下载的文件大小约为12kb,应该为52mb。
当我使用命令wget http://mirror.switch.ch/eclipse/che/che-ls-jdt/snapshots/che-jdt-language-server-latest.tar.gz
时,文件下载成功。
用Node调用的代码有什么问题?为什么我会收到HTTP 400?