如何使用节点将多部分表单数据发布到rails后端?

时间:2015-07-07 17:17:17

标签: ruby-on-rails node.js

Node使用这样的代码来提交多部分表单数据:

var form = new FormData();

form.append(project.name+'.zip', fs.createReadStream("/"+project.name+'.zip'));
form.submit({
    host: 'http://host.mysite.dev',
    path: '/api/v1/prototypes',
    headers: {
      'Authentication': 'secret'
    }
  }, function(err, res) {
    if(err) console.log("ERR: ", err)
    else console.log("RES: ", res)
    console.log(res.statusCode);
  });

我的后端希望请求看起来像这样(用邮递员测试)。

------WebKitFormBoundarywo69VE86H8vMc9ah
Content-Disposition: form-data; name="attachment"; filename="basic.zip"
Content-Type: application/zip


------WebKitFormBoundarywo69VE86H8vMc9ah--

相反,我的请求看起来像铁轨无法解释的这种疯狂的胡言乱语。我不知道如何使用节点进行正常的多部分表单请求。

{
"attachment":{
  "_overheadLength":157,
  "_valueLength":0,
  "_lengthRetrievers":[
     null
  ],
  "writable":false,
  "readable":true,
  "dataSize":0,
  "maxDataSize":2097152,
  "pauseStreams":true,
  "_released":false,
  "_streams":[
     "----------------------------860185489573893683941879\r\nContent-Disposition: form-data; name=\"app.zip\"; filename=\"app.zip\"\r\nContent-Type: application-zip\r\n\r\n",
   {
        "source":{
           "_readableState":{
              "objectMode":false,
              "highWaterMark":65536,
              "buffer":[
                 {
                    "type":"Buffer",
                    "data":[
                       80,
                       181
                    ]
                 }
              ],
              "length":65536,
              "pipes":null,
              "pipesCount":0,
              "flowing":false,
              "ended":false,
              "endEmitted":false,
              "reading":false,
              "sync":false,
              "needReadable":false,
              "emittedReadable":true,
              "readableListening":false,
              "defaultEncoding":"utf8",
              "ranOut":false,
              "awaitDrain":0,
              "readingMore":false,
              "decoder":null,
              "encoding":null
           },
           "readable":true,
           "domain":null,
           "_events":{

           },
           "_eventsCount":3,
           "path":"/Users/guest/Library/Application Support/foundation-apps-template/app.zip",
           "fd":299,
           "flags":"r",
           "mode":438,
           "autoClose":true
        },
        "dataSize":0,
        "maxDataSize":null,
        "pauseStream":true,
        "_maxDataSizeExceeded":false,
        "_released":false,
        "_bufferedEvents":[

        ],
        "_events":{

        },
        "_eventsCount":1
     },
     null
  ],
  "_currentStream":null,
  "_boundary":"--------------------------860185489573893683941879"
 }
}

我不知道该怎么做A.解析这个疯狂的数据包(我不知道该怎么做)。或者B.让它看起来像普通的多部分表单提交。我已经坚持这个问题3个工作日了,我已经尝试了很多东西,其中没有一个有效。我开始不顾一切地想出这个。我已经准备好放弃所有这些并开始从头开始组装我的数据包 - 不知道如何做到这一点。但在这一点上,要想做出无意义的工作,就不会有任何困难。

0 个答案:

没有答案