You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用URLRequest发起POST请求遇405错误,Postman请求返回500求助

问题:POST请求Vercel服务器出现405/500错误调试

我尝试向新部署的Vercel服务器发起POST请求,在Postman中请求时出现500错误,而我的Swift代码仅返回405错误。以下是我的代码:

func callRequest() {
    let full_path: String = "https://suno-api-six-henna.vercel.app/api/generate"
    let json: [String: Any] = ["prompt": "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously.",
                               "make_instrumental": "false", "wait_audio": "true"]
    
    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    if let url = URL(string: full_path) {
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
        request.httpBody = jsonData

        do {
            let task = URLSession.shared.dataTask(with: url) { data, response, error in
                //This converts the optionals in to non optionals that could be used further on
                //Be aware this will just return when something goes wrong
                guard let data = data, let response = response, error == nil else{
                    print("Something went wrong: error: \(error?.localizedDescription ?? "unkown error")")
                    return
                }
                print(response)
                let test = String(decoding: data, as: UTF8.self)
                print(test)
            }
            
            task.resume()
        }
    }
}

我想调试问题,会不会是请求头的问题?


问题分析与解决方案

1. Swift代码返回405的核心原因

你的代码中构造了正确的POST请求对象,但调用dataTask时传入的是原始url而非自定义的request,这会导致URLSession默认发送GET请求,而服务器的/api/generate接口不支持GET方法,因此返回405(Method Not Allowed)错误。

修复代码

把URLSession.shared.dataTask(with: url)修改为:

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    // 原回调逻辑不变
}

2. Postman返回500错误的排查方向

Postman的500是服务器内部错误,和Swift的405是独立问题,可能的原因包括:

  • 参数格式不匹配:Swift代码中make_instrumental和wait_audio传的是字符串"false"/"true",但服务器可能期望布尔值(不带引号的true/false),导致解析出错
  • 服务器端逻辑错误:Vercel部署的函数代码存在bug,比如处理请求体、调用第三方服务时出错
  • 冷启动或资源限制:Vercel函数首次请求冷启动时可能出现超时或资源不足,导致内部错误

3. 额外优化建议

  • 完善错误处理:替换try? JSONSerialization.data(withJSONObject: json)为显式错误处理,避免请求体为空却无法察觉:
    do {
        let jsonData = try JSONSerialization.data(withJSONObject: json)
        request.httpBody = jsonData
    } catch {
        print("JSON序列化失败:\(error.localizedDescription)")
        return
    }
    
  • 添加请求日志:打印请求的httpMethod和请求头,确认实际发送的请求是否符合预期:
    print("请求方法:\(request.httpMethod ?? "未知")")
    print("请求头:\(request.allHTTPHeaderFields ?? [:])")
    
  • 查看服务器日志:登录Vercel控制台,查看对应函数的执行日志,这是定位500错误具体原因最直接的方式

内容的提问来源于stack exchange,提问作者Mr. Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:32:32