代码之家  ›  专栏  ›  技术社区  ›  basith

nodejs-使用unirest发布带有表单数据的请求

  •  1
  • basith  · 技术社区  · 6 年前

    postman request

    如何在node.js中使用unirest发布文件(请参考我的屏幕截图)。我经历过联合国教科文组织 doc 发现可以使用下面的代码将表单数据发送到给定的URL

    unirest.post('http://mockbin.com/request')
    .headers({'Content-Type': 'multipart/form-data'})
     .field('parameter', 'value') // Form field
    .attach('file', '/tmp/file') // Attachment
    .end(function (response) {
     console.log(response.body);
    });
    

    请看一下所附的截图。需要将密钥名称命名为“html”。

    如何将同一邮差请求导出到node.js(unirest)

    1 回复  |  直到 6 年前
        1
  •  2
  •   Arif Khan codemt    6 年前

    .attach('file', '/tmp/file') ,第一个参数是字段名(根据您的键名),第二个参数是文件路径,可以通过以下方式传递

    var unirest = require('unirest');
    
    unirest.post('http://localhost:3000/api/addProject/')
    .headers({'Content-Type': 'multipart/form-data'})
    .attach('html', 'D:\\data\\index.html') // Attachment
    .end(function (response) {
      console.log(response.body);
    });