internal server error (500)
在使用AFNetworking发送delete请求时报下面的错误。
Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)
原因:请求头中添加了
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
服务器端过滤掉该请求,所以报错。
解决方法:
方案一、去掉Content-Type请求头。
方案二、让服务器端该。
服务器端大概代码
fastify.addContentTypeParser('*', (req, done) => {
let data = ''
req.on('error', err => { done(err) })
req.on('data', chunk => { data += chunk })
req.on('end', () => {
done(null, data || {})
})
})
fastify.addHook('onRequest', (req, res, next) => {
// fix空的body的bug
if (includes(bodyMethods, req.method)) {
if (req.headers['content-type'] && req.headers['content-type'].indexOf('application/json') > -1) {
if (!req.headers['content-length'] || req.headers['content-length'] < 2) {
req.headers['content-type'] = 'application/vnd.empty+json'
}
}
}
next()
})