文档首页/ 函数工作流 FunctionGraph/ 常见问题/ 触发器管理/ 使用APIG触发器调用一个返回String的FunctionGraph函数,报500错误,该如何解决?
更新时间:2022-08-19 GMT+08:00

使用APIG触发器调用一个返回String的FunctionGraph函数,报500错误,该如何解决?

FunctionGraph函数对来自APIG调用的返回结果进行了封装,APIG触发器要求函数的返回结果中必须包含body(String)、statusCode(int)、headers(Map)和isBase64Encoded(boolean),才可以正确返回。

Node.js函数APIG触发器调用返回结果定义示例如下:

exports.handler = function (event, context, callback) {
    const response = {
        'statusCode': 200,
        'isBase64Encoded': false,
        'headers': {
            "Content-type": "application/json"
        },
        'body': 'Hello, FunctionGraph with APIG',
    }
    callback(null, response);
}

Java函数APIG触发器调用返回结果定义示例如下:

import java.util.Map;

public HttpTriggerResponse index(String event, Context context){
        String body = "<html><title>FunctionStage</title>"
                + "<h1>This is a simple APIG trigger test</h1><br>"
                + "<h2>This is a simple APIG trigger test</h2><br>"
                + "<h3>This is a simple APIG trigger test</h3>"
                + "</html>";
        int code = 200;
        boolean isBase64 = false;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "text/html; charset=utf-8");
        return new HttpTriggerResponse(body, headers, code, isBase64);
    }


class HttpTriggerResponse {
    private String body;
    private Map<String, String> headers;
    private int statusCode;
    private boolean isBase64Encoded;
    public HttpTriggerResponse(String body, Map<String,String> headers, int statusCode, boolean isBase64Encoded){
        this.body = body;
        this.headers = headers;
        this.statusCode = statusCode;
        this.isBase64Encoded = isBase64Encoded;
    }
}