OpenAI GPT-3 API error: “You must provide a model parameter“

题意:OpenAI GPT-3 API 错误:"你必须提供一个模型参数"

问题背景:

I am trying to POST a question to openAI API via SWIFT. It works fine, if I use the same payload via Postman, but in the Xcode-Condole I got the following response from openAI:

我正在尝试通过 Swift 向 OpenAI API 发送一个 POST 请求。如果我在 Postman 中使用相同的负载,它工作正常,但在 Xcode 控制台中,我从 OpenAI 收到了以下响应:

Swift 复制代码
Response data string:
{
     "error": {
         "message": "you must provide a model parameter",
         "type": "invalid_request_error",
         "param": null,
         "code": null
    }
 }

This is my code: 以下是我的代码

Swift 复制代码
 func getActivityAnalysis(){
    
    let url = URL(string: "https://api.openai.com/v1/completions")
    guard let requestUrl = url else { fatalError() }
    
    // Prepare URL Request Object
    var request = URLRequest(url: requestUrl)
    request.setValue("Bearer blaaaablaa", forHTTPHeaderField: "Authorization")
    request.httpMethod = "POST"
    
    
    let prompt = "just a test"
    let requestBody = OpenAIRequest(model: "text-davinci-003", prompt: prompt, max_tokens: 300, temperature: 0.5)
    
    let encoder = JSONEncoder()
    encoder.outputFormatting = .prettyPrinted
    let data = try! encoder.encode(requestBody)
    print(String(data: data, encoding: .utf8)!)
    
     
    // Set HTTP Request Body
    request.httpBody = data
    
    print("\(request.httpMethod!) \(request.url!)")
    print(request.allHTTPHeaderFields!)
    print(String(data: request.httpBody ?? Data(), encoding: .utf8)!)
    
    
    
    // Perform HTTP Request
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            
            // Check for Error
            if let error = error {
                print("Error took place \(error)")
                return
            }
     
            // Convert HTTP Response Data to a String
            if let data = data, let dataString = String(data: data, encoding: .utf8) {
                print("Response data string:\n \(dataString)")
                self.openAIResponse = dataString
            }
    }
    task.resume()
    
}`

If I print the http request, it seems fine for me as well:

如果我打印出 HTTP 请求,它看起来也没有问题:

Swift 复制代码
 POST https://api.openai.com/v1/completions
 ["Authorization": "Bearer blaaaaa"]
 {
    "temperature" : 0.5,
    "model" : "text-davinci-003",
    "prompt" : "just a test",
    "max_tokens" : 300
 }

I tried to use the same payload in my Postman request. It worked fine here. I also tried to use different encodings, but it always throws the same error.

我尝试在 Postman 请求中使用相同的负载,它在这里工作正常。我还尝试了不同的编码方式,但总是抛出相同的错误。

Not sure, what I am doing wrong. Maybe someone can help?

不确定我哪里做错了。也许有人能帮忙?

Thank you in advance. 提前感谢你。

问题解决:

Your HTTP request reveals the problem. You need to add 'Content-Type: application/json'.

According to GeeksforGeeks:

你的 HTTP 请求暴露了问题。你需要添加 `'Content-Type: application/json'`。

根据 GeeksforGeeks 的说法:

Content-Type is an HTTP header that is used to indicate the media type of the resource and in the case of responses, it tells the browser about what actually content type of the returned content is.

相关推荐
AI 编程助手GPT2 小时前
Bun 1.4 正式发布:从 Zig 改写为 Rust,内置浏览器、图片处理和并行测试
开发语言·人工智能·后端·ai·chatgpt
孤狼GPT2 小时前
Codex额度快用完时,要不要换Luna?什么任务适合降模型,什么任务绝对别降?
chatgpt·codex·luna·chatgpt plus·chatgpt pro·codex额度
顿哥GPT3 小时前
2026-08-28|ChatGPT Plus 与 Codex 实战:用 AI 编程为 Python 接口编写自动化测试的完整复盘
人工智能·chatgpt·重构
DeepIntelli3 小时前
GEO 服务商选型:引用溯源与效果监测该看哪些工程能力
人工智能·chatgpt
全栈弄潮儿11 小时前
用 AI 辅助代码审查:提交前检查什么
aigc·openai·ai编程
像风一样自由202012 小时前
新版ChatGPT (Codex) 桌面版启动失败排查与修复记录
chatgpt·大模型
Flynt15 小时前
Codex回退对话却不回退文件:OpenAI做了两次都没搞定的坑,有人用第三种思路补上了
openai·ai编程
v:ychya201816 小时前
2026 外贸 GEO 实操:4 步让独立站被 ChatGPT 优先引用
人工智能·chatgpt
JavaPub-rodert16 小时前
ChatGPT Windows 客户端突然打不开?提示找不到 Codex CLI,我这样解决了
windows·chatgpt
杨杨杨大侠18 小时前
MCP、ToolFunction 与 Skill:从模型输入到工具执行
后端·aigc·openai