使用CURL调用OctoAI图片放大接口遇报错求助
解决OctoAI图片放大接口的CURL调用问题
问题1:Base64临时文件传递解码失败
报错{"message":"Error decoding provided base64 image in 'init_image' field. Base64 string is not a valid image."},核心原因是生成的Base64字符串含换行符、多余前缀,或是CURL传递方式有误。
解决步骤:
- 生成纯净Base64字符串:用
-w 0参数禁止自动换行,避免字符串被分割导致解码失败:
注意:不要给Base64添加base64 -w 0 input.png > temp_base64.txtdata:image/png;base64,这类前缀,接口仅需纯Base64内容。 - 正确构造JSON请求:用
jq组装JSON(避免手动拼接的转义问题),再通过CURL发送:
也可直接读取文件到JSON字段(需确保Base64无特殊字符):curl -X POST https://api.octoai.run/v1/images/upscale \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d "$(jq -n --arg img "$(cat temp_base64.txt)" '{init_image: $img, width: 1024}')"curl -X POST https://api.octoai.run/v1/images/upscale \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d "{\"init_image\":\"$(cat temp_base64.txt)\",\"width\":1024}"
问题2:大图片Base64传参触发Argument list too large
这是系统命令行参数长度有限制导致的,大图片的Base64字符串过长,超出了参数长度上限。
解决步骤:
用临时JSON文件存储请求体,让CURL直接读取文件内容,绕过命令行参数长度限制:
- 生成纯净Base64(同问题1第一步)
- 生成完整请求体JSON文件:
jq -n --arg img "$(cat temp_base64.txt)" '{init_image: $img, width: 1024}' > request.json - 用CURL发送请求:
curl -X POST https://api.octoai.run/v1/images/upscale \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d @request.json
简化写法:
无需单独生成Base64临时文件,一步生成JSON并发送:
jq -n --arg img "$(base64 -w 0 input.png)" '{init_image: $img, width: 1024}' > request.json curl -X POST https://api.octoai.run/v1/images/upscale \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d @request.json
内容的提问来源于stack exchange,提问作者Pedro Emanuel
相关产品推荐
相关产品推荐

