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.

相关推荐
初级代码游戏6 分钟前
iOS开发 Swift 速记5:高级运算符
ios·移动开发·swift
初级代码游戏39 分钟前
iOS开发 Swift 速记4:函数与闭包
开发语言·ios·swift
9i编程1 小时前
手敲重构学透 Multi-Agent 代码(下):从 AgentScope 1.0.8 升级到 2.0,API 变了什么、踩了哪些坑
人工智能·openai·ai编程
月十丶1 小时前
Skill 越装越多越乱?需要调用时又不知道该调用哪个?我给 Agent 加了一层能力决策层
chatgpt
云卷云舒___________2 小时前
传OpenAI将发Astra新一代模型、谷歌Gemini3.5Pro现身后端、字节欲练5万亿参数大模型 | 8月7日 AI日报
大模型·openai·谷歌·字节跳动·astra·ai日报·gemini35pro
深蓝AI2 小时前
让 AI 少说废话:95K Star 的 Caveman,把输出 token 砍掉 65% 还更准了
chatgpt
王莹月3 小时前
生图API 从 OpenAI 迁到甜甜圈API 要改多少代码?实测只动一行,还多了 nano banana pro / Veo 3.1
gpt·ai·chatgpt·ai作画·aigc·agi
AI产品库4 小时前
2026年8月双响:ChatGPT免费版文本不限量,Qwen3.8-Max开启开源旗舰时代
chatgpt·开源
VIP_CQCRE4 小时前
用 Ace Data Cloud 接入 Codex:让 AI 编程工具配置更简单
openai·ai编程·开发工具·codex·acedatacloud
ZacJi15 小时前
当苹果将系统 API 划归 @MainActor:一次跨 SDK 版本的 Swift 并发踩坑与破局实战
ios·swift