2015-06-24 47 views
1

我已在請求中的以下被從瀏覽器發送:request.body總是空的多部分形式數據

Remote Address:127.0.0.1:80 
Request URL:http://doctor.com/api/v2/chat/message 
Request Method:POST 
Status Code:501 Not Implemented 
**Response Headers** 
view source 
Access-Control-Allow-Headers:Content-Type,X-Requested-With 
Access-Control-Allow-Methods:POST, GET, PUT, DELETE, OPTIONS 
Access-Control-Allow-Origin:* 
Connection:keep-alive 
Content-Length:61 
Content-Type:text/html; charset=utf-8 
Date:Wed, 24 Jun 2015 11:16:33 GMT 
ETag:W/"3d-70662653" 
X-Powered-By:Express 
**Request Headers** 
view source 
Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate 
Accept-Language:en-US,en;q=0.8 
Cache-Control:no-cache 
Connection:keep-alive 
Content-Length:39235 
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary3qsbh041bbj3MYfd 
Cookie:serviceToken=558a86bb69f3197ab93fd64c 
DNT:1 
Host:doctor.com 
Origin:http://doctor.com 
Pragma:no-cache 
Referer:http://doctor.com/platform/chat 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36 
Request Payload 
------WebKitFormBoundary3qsbh041bbj3MYfd 
Content-Disposition: form-data; name="id" 

55896d9bc57f69df66284176 
------WebKitFormBoundary3qsbh041bbj3MYfd 
Content-Disposition: form-data; name="attachment"; filename="Screen Shot 2015-06-24 at 3.18.10 am.png" 
Content-Type: image/png 


------WebKitFormBoundary3qsbh041bbj3MYfd-- 

該請求由節點服務器截獲。下面是它的外觀:

var express = require('express'); 
var http = require('http'); 
var bodyParser = require('body-parser'); 
var cookieParser = require('cookie-parser'); 
var fs = require('fs'); 
var path = require('path'); 
var request = require('request'); 
var _ = require('underscore-node'); 
var express = require('express'); 

app.use(bodyParser.json()); 
app.use(cookieParser()); 
app.use(bodyParser.urlencoded({extended:false})); 

app.use('/api/*', function (req, res, next) { 
    console.log(req.body); 
}); 

問題是,我總是將req.body設置爲空。它在json發佈時工作正常。

+0

作爲除此之外,我假設這裏有一個複製/粘貼錯誤:你正在多次設置你的變量(express,http,bodyParser等)。更新了 – Ben

+0

!謝謝。 – beNerd

+0

body-parser無法分析多部分表單數據。你需要一個單獨的中間件來做到這一點。 –

回答

2

從body-parser文檔:https://github.com/expressjs/body-parser 您需要一個額外的中間件。

這不處理多部分機構,由於其複雜和 通常大型性質。對於多機構,你可能有興趣在 以下模塊:

  • 打雜和連接,打雜
  • 多方並連接多黨
  • 強大
  • multer