2014-09-06 63 views
0

文檔插入REST API(POST/v1/documents)需要multipart/mixed用於內容類型。所有示例在線顯示如何使用多部分/表單數據。根據我的研究,我瞭解到多部分/混合需要嵌入到多部分/表單數據中。有人可以請我指出一個例子或資源,我可以得到一個線索?謝謝!如何將多部分/表單數據轉換爲多部分/混合

供參考:我使用的是前端和Node.js的在後端AngularJS

角碼:

  $http({ 
       url: '/new', 
       method: 'POST', 
       headers: { 
        'Content-Type': undefined 
       }, 
       transformRequest: function(data, getHeaders) { 
        var form = new FormData(); 
        form.append('bug', angular.toJson(bug)); 
        for (var i = 0; i < data.files.length; i++) { 
         console.log('FORM',data.files[i]); 
         form.append('file' + i, data.files[i]); 
        } 
        return form; 
       }, 
       data: { 
        bug: bug, 
        files: $scope.files 
       } 
      }).success(function(data, status, headers, config) { 
       Flash.addAlert('success', 'successssss'); 
      }).error(function(data, status, headers, config) { 
       Flash.addAlert('danger', 'failed'); 
      }); 

節點代碼::使用request 注:本代碼肯定是錯誤的,因爲它將數據視爲json而不是mulitpart/mixed,我不知道,因此問題

.... 
....  
case 'POST': 
      console.log('its a POST'); 
      var url = 'http://api-server.com:8003/v1/documents?extension=json'; 
      var options = { 
       method: 'POST', 
       headers: req.headers, 
       url: url, 
       body: JSON.parse(req.body.bug), 
       json: true 
      }; 

      req.pipe(request(options, function(error, response, body) { 
       if (error) { 
        next(error); 
       } 
      })).pipe(res); 

這裏的多/表單數據,我得到目前

------WebKitFormBoundaryJlYMd1KVllv1WgDS 
Content-Disposition: form-data; name="bug" 

{"relatedTo":[],"tickets":[],"id":1,"kind":"Other","createdAt":"2014-09-06T08:33:43.614Z","modifiedAt":"2014-09-06T08:33:43.614Z","status":"Verify","title":"Quae inventore beatae tempora mollit deserunt voluptatum odit adipisci consequat Est dolore quia perspiciatis","submittedBy":{"name":"Sudhakar Reddy","email":"[email protected]","username":"sreddy"},"assignTo":{"name":"Guzman Wagner","email":"[email protected]","username":"small"},"description":"sdsdsdsds","category":"MLOS","tofixin":"Help-1.1","severity":"Performance","priority":{"level":"4","title":"Important"},"relation":"Test Specification task for","clones":[],"version":"6.0-3","platform":"EC2","memory":"Reprehenderit quia aut voluptatem in ex dolore eu numquam eum et esse officia id consequatur Est","processors":"Reiciendis nostrum adipisicing occaecat inventore veniam excepturi","note":"Officiis qui adipisci commodo eveniet, esse aperiam est non unde possimus, sed nesciunt, exercitation eius magna consequat. Sint ipsa, laboriosam.","changeHistory":[],"subscribers":[{"name":"Sudhakar Reddy","email":"[email protected]","username":"sreddy"},{"name":"Guzman Wagner","email":"[email protected]","username":"small"}],"attachments":[{"webkitRelativePath":"","lastModifiedDate":"2014-07-18T23:53:29.000Z","name":"jamesbond.jpg","type":"image/jpeg","size":858159}]} 
------WebKitFormBoundaryJlYMd1KVllv1WgDS 
Content-Disposition: form-data; name="file0"; filename="jamesbond.jpg" 
Content-Type: image/jpeg 


------WebKitFormBoundaryJlYMd1KVllv1WgDS-- 

回答

0

不同multipart/*內容類型之間的區別只是語義,只需修改Content-Typereq.headers將請求發送到REST端點之前:

var url = 'http://api-server.com:8003/v1/documents?extension=json'; 

// if you use `req.headers` elsewhere, you may want to make a copy of the 
// headers object so as not to mutate the original headers ... 
req.headers['content-type'] = req.headers['content-type'] 
           .replace('multipart/form-data', 
              'multipart/mixed'); 

var options = { 
    method: 'POST', 
    headers: req.headers, 
    url: url, 
    body: JSON.parse(req.body.bug), 
    json: true 
}; 

req.pipe(request(options, function(error, response, body) { 
    if (error) { 
     next(error); 
    } 
})).pipe(res); 
+0

使用您的解決方案後,請求未被提交。在Chrome調試器中的標題顯示'小心:臨時標題顯示' – Sudhakar 2014-09-06 19:01:36

+1

也如何設置每個部分的內容處置標題 例如:我需要這樣的內容:'Content-Disposition:attachment;文件名=「doc2.json」'但目前我不知道如何改變這一點 – Sudhakar 2014-09-06 22:47:26

相關問題