Why is OpenAI image generation Api returning 400 bad request in Unity?

题意:为什么 OpenAI 图像生成 API 在 Unity 中返回 400 Bad Request 错误?

问题背景:

I'm testing out dynamically generating images using OpenAI API in Unity. Amusingly, I actually generated most of this code from chatGPT.

我正在测试在 Unity 中使用 OpenAI API 动态生成图像。有趣的是,我实际上是通过 ChatGPT 生成了大部分代码。

The Error response is: "Your request contained invalid JSON: Expecting value: line 1 column 1 (char 0)". But I cant see anything wrong with the json formatting of my requestbody...

错误响应是:'你的请求包含无效的 JSON:期待值:第 1 行第 1 列 (字符 0)'。但我看不出我的请求体的 JSON 格式有什么问题......

I also found this other question which is probably failing for the same reason, whatever it is:

我还发现了另一个问题,可能因为同样的原因而失败,无论是什么原因:Why does Post request to OpenAI in Unity result in error 400?

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

复制代码
public class PlayerScript : MonoBehaviour
{
    // Replace API_KEY with your actual API key
    private string API_KEY = "<api_key>";
    private string API_URL = "https://api.openai.com/v1/images/generations";

    void Start()
    {
        StartCoroutine(GetImage());
    }

    IEnumerator GetImage()
    {
        // Create a request body with the prompt "Player"
        string requestBody = "{\"prompt\": \"Player\",\"n\": 1,\"size\": \"128x128\"}";
        // Create a UnityWebRequest and set the request method to POST
        UnityWebRequest www = UnityWebRequest.Post(API_URL, requestBody);

        // Set the authorization header with the API key
        www.SetRequestHeader("Authorization", "Bearer " + API_KEY);

        // Set the content type header
        www.SetRequestHeader("Content-Type", "application/json");

        // Send the request
        yield return www.SendWebRequest();

        // Check for errors
        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }
        else
        {
            // do stuff
        }
    }
}

Any idea what's going wrong? Apologies if it's something obvious, never made web requests in Unity before.

知道哪里出了问题吗?如果问题显而易见请见谅,我以前从未在 Unity 中发过网络请求。

问题解决:

UnityWebRequests url-encodes content before submitting the request. This messes up the formatting. see:

UnityWebRequests 在提交请求之前会对内容进行 URL 编码,这会导致格式混乱。参见:prevent UnityWebRequest.Post() from Url-Encoding the data ? - Questions & Answers - Unity Discussions

using httpClient worked for me.

"使用 httpClient 对我来说是有效的。

相关推荐
菩提小狗9 小时前
每日极客日报 · 2026年05月01日
ai·开源·极客日报·it热点·技术资讯
蔡俊锋10 小时前
AI是一面镜子
人工智能·ai·规格说明书·ai是一面镜子
阿源-11 小时前
Claude Code在Windows/WSL-Linux/VS Code三平台上的安装配置参考
ai·编程工具
Joseph Cooper14 小时前
AI Agent 落地入门:从模型、工具到 Skills 与 MCP 的分工
人工智能·ai·agent·claude·skill·mcp
宝桥南山15 小时前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
无心水15 小时前
【Hermes:Skill系统深度】22、资产保值时代:OpenClaw Skill → Hermes 无缝迁移完整指南
人工智能·ai·openclaw·养龙虾·hermes·养马·honcho
刘大猫.15 小时前
宝马发布全新AI智能座舱助手 能理解用户复杂出行需求
人工智能·算法·机器学习·ai·大模型·算力·ai智能座舱助手
行者-全栈开发16 小时前
Spring AI + GPT-4 实战:API Key 安全管理与企业级集成方案(避坑指南)
openai api·错误处理·密钥管理·spring ai·企业级开发·请求封装·api 安全
维元码簿16 小时前
Claude Code 深度拆解:多 Agent 协作 2 — 上下文隔离与权限边界
ai·agent·claude code·ai coding
笨蛋©16 小时前
2026制造业实战:ISO 9001认证体系下的检验计划数字化与图纸识别流程
ai·cad·质量管理·制造业·图纸识别