当我尝试从iOS应用程序访问我在节点中创建的Express端点时,出现413错误,说有效负载太大。这是我的节点代码。
router.post('/images/tags/nutritionalInformation/image/base64encoded', function (req, res) {
var base64Stream = req.body.imageString;
var imgDecodedBuffer = decodeBase64Image(base64Stream);
// write to image file
var prefix = guid().toString() + ".jpg";
var filePath = './uploads/' + prefix;
return s3fsImpl.writeFile(prefix, imgDecodedBuffer.data).then(function () {
var s3BaseUrl = "https://s3.amazonaws.com/app/" + prefix;
console.log(s3BaseUrl);
app.models.predict('KEY', s3BaseUrl).then(
function (response) {
var responseJson = JSON.stringify(response.data.outputs[0].data.concepts);
var data = collectTags(responseJson);
data.then(function (value) {
res.json(value);
});
},
function (err) {
console.log(err);
});
});
})
我在postman中测试了这个,当我在体内附加base64编码的字符串时,它工作正常!但是,当我尝试使用以下代码从Swift发送POST调用时:
func getTags() {
let image : UIImage = UIImage(named: "burger.jpg")!
let imageData = UIImageJPEGRepresentation(image, 1.0)
// base64 encode imagedata
let prefix = "data:image/jpeg;base64,"
let strBase64:String = prefix + (imageData?.base64EncodedString(options: .lineLength64Characters))!
// Call tagging api https://foodenodeapp.herokuapp.com/api/images/tags/nutritionalInformation/image/base64encoded
var request = URLRequest(url: URL(string: "https://foodenodeapp.herokuapp.com/api/images/tags/nutritionalInformation/image/base64encoded")!)
request.httpMethod = "POST"
let poststring = "imageString=" + strBase64
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
print(data)
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
但是,当我在Swift Code中打印出responseString时,我得到了这个回复:
nutritionalInformation/image/base64encoded } { status code: 413, headers {
Connection = "keep-alive";
"Content-Length" = 18;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 04 Dec 2016 20:18:15 GMT";
Server = Cowboy;
Via = "1.1 vegur";
"X-Content-Type-Options" = nosniff;
"X-Powered-By" = Express;
} }
18 bytes
responseString = Optional("Payload Too Large\n")
所以我做了一些研究,然后尝试更改我的app.js
,这是我的根文件(逻辑代码在api.js
中)。
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({extended:true, limit: '50mb'}));
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.redirect('/api');
});
app.use('/api', require('./routes/api'));
但是,在此更改之后,当我在Swift中运行该函数时,我现在得到500错误。
非常感谢任何解决此问题的帮助!
答案 0 :(得分:0)
从服务器端发生此问题。
如果您的服务器为Node JS
,请进行更改: -
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json({ limit: '10mb' })) // change limit according to you.
用于其他服务器更改限制。