gpt4 book ai didi

node.js - 从 Swift 调用 Express Api 时出现 413 错误

转载 作者:行者123 更新时间:2023-11-30 12:51:05 27 4
gpt4 key购买 nike

当我尝试从 iOS 应用程序访问我在 Node 中创建的 Express 端点时,收到 413 错误,指出负载太大。这是我的 Node 代码。

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 代码中的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 错误。

任何解决此问题的帮助将不胜感激!

最佳答案

此问题发生在服务器端。

如果您的服务器是 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.

对于其他服务器更改限制。

关于node.js - 从 Swift 调用 Express Api 时出现 413 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40967586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com