陈海宏5258
陈海宏5258
  • 发布:2023-02-09 16:13
  • 更新:2023-02-13 11:28
  • 阅读:292

【报Bug】云函数,集成响应不支持设置content-type为image/png和image/svg+xml

分类:uniCloud

产品分类: uniCloud/App

App下载地址或H5⽹址: https://xxx.cn/

示例代码:

content-type为image/png,返回的内容,

{  
  mpserverlessComposedResponse: true,  
  isBase64Encoded: true,  
  statusCode: 200,  
  headers: {  
    'content-type': 'image/png'  
  },  
  body: 'base64编码后的图片字符串',  
}

content-type为image/svg+xml,返回的内容,

{  
  mpserverlessComposedResponse: true,  
  statusCode: 200,  
  headers: {  
    'content-type': 'image/svg+xml'  
  },  
  body: 'svg字符串',  
};  

操作步骤:
  • 访问地址 https://xxx.cn/pages/setting/loginH5/loginH5
  • 无法正常显示登录二维码。

预期结果:

能正常显示登录二维码,即:

  • 云函数能准确响应设置的content-type为我设置的image/png。
  • 云函数能准确响应设置的content-type为我设置的image/svg+xml。

实际结果:

云函数响应的content-type变为了application/json。

bug描述:

从免费版升级正式版后,集成相应不支持自定义content-type了。
升级之前,我设置了content-type为image/png,之前能正常响应我设置的类型。
升级后,拿到的响应类型都是application/json。
我又尝试设置了image/svg+xml,前面几天是可以的,现在又不行了,响应类型也是application/json了。

这里是文档地址:https://uniapp.dcloud.net.cn/uniCloud/http.html#integrationresponse

2023-02-09 16:13 负责人:无 分享
已邀请:
陈海宏5258

陈海宏5258 (作者) - 我是人。

有官方的大佬解答下吗?
现在,集成响应支不支持自定义content-type?

DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

我这测试是可以正常响应上面两种content-type的,猜测可能是代码存在其他报错导致返回的内容不符合预期,可以写个简单的示例试试看能不能正常返回。帖子内的域名我给你打个码

'use strict';  
const fs = require('fs')  
const path = require('path')  
exports.main = async (event, context) => {  
  const base64 = fs.readFileSync(  
    path.resolve(__dirname, 'test.svg')  
  ).toString('base64')  
  return {  
    "mpserverlessComposedResponse": true,  
    "isBase64Encoded": true,  
    "statusCode": 200,  
    "headers": {  
      "content-type": 'image/svg+xml'  
    },  
    "body": base64  
  }  
};
  • 陈海宏5258 (作者)

    你好,我尝试了,不行。我用了base64编码与未编码的两种方式,都不行。


    exports.main = (event, context) => {  
    return {
    "mpserverlessComposedResponse": true,
    "isBase64Encoded": true,
    'statusCode': 404,
    'headers': {
    'content-type': 'text/html',
    },
    'body': new Buffer('<h1>404</h1>').toString('base64'),
    };

    return {
    "mpserverlessComposedResponse": true,
    "isBase64Encoded": false,
    'statusCode': 404,
    'headers': {
    'content-type': 'text/html',
    },
    'body': '<h1>404</h1>',
    };
    };

    2023-02-13 13:56

  • DCloud_uniCloud_WYQ

    回复 陈海宏5258: statusCode改成200试试

    2023-02-13 14:13

  • 陈海宏5258 (作者)

    回复 DCloud_uniCloud_WYQ: 不行

    2023-02-13 14:42

要回复问题请先登录注册