简体   繁体   English

如何使用node.js请求模块发送文件?

[英]How do I send a file with node.js request module?

I am trying to use an API to update a list on another server using node.js. 我正在尝试使用API​​使用node.js在另一台服务器上更新列表。 For my last step, I need to send a POST that contains a csv file. 对于我的最后一步,我需要发送一个包含csv文件的POST。 In the API, they list under FormData that I need a Key called file and a Value of Binary Upload, then the body of the request should be made of listname: name and file: FileUpload. 在API中,它们在FormData下列出了​​我需要一个称为file的Key和一个Binary Upload的值,然后请求的主体应由listname组成:name和file:FileUpload。

function addList(token, path, callback) {

//Define FormData and fs
var FormData = require('form-data');
var fs = require('fs');

//Define request headers.
headers = {
    'X-Gatekeeper-SessionToken': token,
    'Accept': 'application/json',
    'Content-Type': 'multipart/form-data'
};

//Build request.
options = {
    method: 'POST',
    uri: '{URL given by API}',
    json: true,
    headers: headers
};

//Make http request.
req(
    options,
    function (error, response, body) {
        //Error handling.
        if (error) { callback(new Error('Something bad happened')); }

        json = JSON.parse(JSON.stringify(response));
        callback.call(json);
    }
);

//Attempt to create form and send through request
var form = new FormData();
form.append('listname', 'TEST LIST');
form.append('file', fs.createReadStream(path, { encoding: 'binary' }));
form.pipe(req);};

I am a veteran of front end javascript for html and css, but this is my first adventure with backend node.js. 我是html和css的前端javascript的资深人士,但这是我第一次使用后端node.js。 The error I keep getting is: TypeError: dest.on is not a function 我一直收到的错误是: TypeError: dest.on is not a function

From what I can tell, this has to do with the way I used form.pipe(req) but I can't find documentation telling me the appropriate usage. 据我所知,这与我使用form.pipe(req)但是我找不到能告诉我适当用法的文档。 If you don't have a direct answer, a finger pointing toward the right documentation would be appreciated. 如果您没有直接的答案,请指着正确的文档。

The issue is you're not passing the request instance into your pipe call, you're passing the request module itself. 问题是您没有将请求实例传递到管道调用中,而是传递了请求模块本身。 Take a reference to the return value of your req(...) call and pass this instead ie 引用您的req(...)调用的返回值,然后传递该值,即

//Make http request.
const reqInst = req(
   options,
   function (error, response, body) {
       //Error handling.
       if (error) { callback(new Error('Something bad happened')); }

       json = JSON.parse(JSON.stringify(response));
       callback.call(json);
   }
);

//Attempt to create form and send through request
var form = new FormData();
...
form.pipe(reqInst);

This line: 这行:

.pipe(fileinclude)

Should be this: 应该是这样的:

.pipe(fileinclude())

from

why am i getting this error? 为什么我会收到此错误? dest.on is not a function - using gulp-file-include dest.on不是函数-使用gulp-file-include

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM