【问题标题】:How to make a post request a file along with JSON data in Node.JS using a request module如何使用请求模块在 Node.JS 中将发布请求与 JSON 数据一起发送到文件
【发布时间】:2018-09-25 13:08:00
【问题描述】:

在下面的 python 中,我的代码正在解决这个问题,但我在 Node.JS 中发现了这个请求模块的困难。

这是我的python代码:

inputData = {
  "datasetName": "dataset44",
  "typeOfDataset": "TS",
  "userID": "c0fe211c_8c41_459a_9019_19d98111ed92"
}
fileName = 'out.csv'
files = {
  'json': (None, json.dumps(inputData), 'application/json'),
  'file': (os.path.basename(fileName), open(fileName, 'rb'),
    'application/octet-stream')
}

我在这里尝试什么:

var fileData = fs.createReadStream(filePath);

var headers = {
    'Content-Type': 'application/json , application/octet- 
    stream,
    multipart / form - data ' }

    var files = {
      'json': {
        "datasetName": dataSetName,
        "typeOfDataSet": "TS",
        "userID": req.session.userSourceID
      },
      'file': fileData
    }
    // Configure the request
    var options = {
      url: ' http://xx.xx.xx.x:x/x1/createx',
      method: 'POST',
      headers: headers,
      json: files
    }

    request(options, function(error, response, body) {

【问题讨论】:

    标签: json node.js post request sails.js


    【解决方案1】:

    您必须在 Node.js 中使用 multipart/related。见https://github.com/request/request#multipartrelated

    【讨论】:

    • 这看起来很有帮助,但我对语法感到困惑。如何设置具有不同内容类型的文件和具有不同内容类型的 json 数据。两者都在一个对象中。
    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多