[英]Node/Request Error: “Processing POST Request: No Content-Type”
I have a front end Canvas that I transform into a png file that I need to POST to a third party vendor's api. 我有一个前端Canvas,可以将其转换为png文件,然后将其发布到第三方供应商的api。 It passes back to node as a base64 file and I decode it, but when I attempt the upload, it gives me the following error:
它作为base64文件传回节点,并对其进行解码,但是当我尝试上传时,它给了我以下错误:
Problem processing POST request: no Content-Type specified
处理POST请求时出现问题:未指定Content-Type
However, I am clearly specifying the content type in my POST call. 但是,我在POST调用中明确指定了内容类型。 My end goal is to upload the file to my vendor's API.
我的最终目标是将文件上传到供应商的API。
Here are the key front end aspects: 以下是关键的前端方面:
var canvasImage = document.getElementById("c");
var img = canvas.toDataURL({
multiplier: canvasMultiplier
});
var fileTime = Date.now();
var myFileName = $scope.productCode + fileTime;
$scope.filenameForVendor = myFileName;
var filename = $scope.filenameForVendor;
$http.post('/postVendor', { filename: filename, file: img }).success(function (data) {
console.log("Uploaded to Vendor");
Here is the backend POST: 这是后端POST:
app.post('/postVendor', function (req, res, next) {
var filename = req.body.filename;
var file = req.body.file;
fileBuffer = decodeBase64Image(file);
request({
url: "http://myvendorapi/ws/endpoint",
method: "POST",
headers: {
'contentType': fileBuffer.type
},
body: fileBuffer.data
}, function (error, response, body) {
console.log(response);
});
})
// Decode file for upload
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
}
I can POST using AJAX on the front end, but because of CORS and the vendor blocking all but server side calls to the endpoints (and they don't have JSONP), I can't use this. 我可以在前端使用AJAX进行POST,但是由于CORS和供应商阻止了所有(除了服务器端)对端点的调用(并且它们没有JSONP),因此无法使用它。 They are allowing my IP through for testing purposes so only I can make this work from my machine:
他们允许我的IP用于测试目的,因此只有我才能在计算机上完成此工作:
var send = function (blob) {
var fileTime = Date.now();
var myFileName = $scope.productCode + fileTime;
$scope.filenameForVendor = myFileName;
var filename = $scope.filenameForVendor;
var formdata = new FormData();
formdata.append('File1', blob, filename);
$.ajax({
url: 'http://myvendorapi/ws/endpoint',
type: "POST",
data: formdata,
mimeType: "multipart/form-data",
processData: false,
contentType: false,
crossDomain: true,
success: function (result) {
console.log("Upload to Vendor complete!");
// rest of code here/including error close out
}
var bytes = atob(dataURL.split(',')[1])
var arr = new Uint8Array(bytes.length);
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes.charCodeAt(i);
}
send(new Blob([arr], { type: 'image/png' }));
Update: 更新:
I realized that contentType should be 'content-type'. 我意识到contentType应该是“ content-type”。 When I did this, it creates an error of no boundary specified as I am trying multipart-form data (which I did all wrong).
当我这样做时,它会产生一个没有边界的错误,因为我正在尝试使用多部分形式的数据(我做错了所有事情)。 How can I pass formData to Node for uploading?
如何将formData传递给Node进行上传?
Update 2: 更新2:
Per the advice offered, I tried using multer but am getting an ReferenceError: XMLHttpRequest is not defined. 根据提供的建议,我尝试使用multer,但遇到了ReferenceError:未定义XMLHttpRequest。
Client side: 客户端:
var fileTime = Date.now();
var myFileName = $scope.productCode + fileTime;
$scope.filenameForVendor = myFileName;
var filename = $scope.filenameForVendor;
var formdata = new FormData();
formdata.append('File1', blob, filename);
$http.post('/postVendor', formdata, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).success(function (data) {
Server side: 服务器端:
app.post('/postVendor', function (req, res, next) {
var request = new XMLHttpRequest();
request.open("POST", "http://myvendorapi.net/ws/endpoint");
request.send(formData);
})
Why do you base64 encode the file? 为什么要对文件进行base64编码?
You can upload raw file to your Node using FormData and you will not have to decode anything. 您可以使用FormData将原始文件上传到您的Node,而无需解码任何内容。
...
var request = new XMLHttpRequest();
request.open('POST', 'http://node.js/method');
request.send(formData); // vanilla
--- or --- - - 要么 - -
...
$http.post('http://node.js/method', formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}); // angular
Just install request . 只需安装请求 。
...
var request = require('request');
app.post('/method', function (req, res, next) {
// if you just want to push request you don't need to parse anything
req.pipe(request('http://vendor.net')).pipe(res);
}) // express
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.