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 对我来说是有效的。

相关推荐
Wise玩转AI5 小时前
Day 26|智能体的“伦理与安全边界”
人工智能·python·安全·ai·chatgpt·ai智能体
带刺的坐椅5 小时前
Solon AI 开发学习7 - chat - 四种消息类型及提示语增强
java·ai·llm·solon
阿杰学AI5 小时前
AI核心知识22——大语言模型之重要参数Top-P(简洁且通俗易懂版)
人工智能·ai·语言模型·aigc·模型参数·top-p
阿杰学AI5 小时前
AI核心知识21——大语言模型之核心参数Temperature(简洁且通俗易懂版)
人工智能·ai·语言模型·aigc·temperature·大模型参数
阿杰学AI6 小时前
AI核心知识23——大语言模型之System Prompt(简洁且通俗易懂版)
人工智能·ai·语言模型·prompt·aigc·system prompt
风华浪浪6 小时前
python 基础之 jsonpatch 用于对 JSON 文档的局部更新操作
linux·python·json
汤姆丁11117 小时前
全面实测Gemini 3.0,前端这回真死了吗?
ai
AlfredZhao7 小时前
Oracle AI:使用SQL来实现融合检索和整个RAG流程
ai·oracle
努力改掉拖延症的小白15 小时前
Intel笔记本也能部署大模型(利用Ultra系列gpu通过优化版ollama实现)
人工智能·ai·语言模型·大模型
AI大模型学徒15 小时前
大模型应用开发(四)_调用大模型分析图片
人工智能·深度学习·ai·大模型·deepseek