代码之家  ›  专栏  ›  技术社区  ›  Amey Waze

如何解析lambda函数作为输入接收的未转义json?

  •  0
  • Amey Waze  · 技术社区  · 6 年前

    我正在使用web抓取工具(parsehub)提取数据。提取完成后,parsehub将有关此数据的信息(以json格式)发送到amazon lambda,我将其用作webhook。但是这个json没有正确转义,因此lambda抛出了一个错误(即无法解析请求体)。如何转义json字符串以使lambda不会抛出错误?我还使用eclipse测试了这个功能。

    我用简单的Java类型作为输入。 https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html )我也试过用pojos( https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html )和字节流实现( https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-stream.html )作为输入,但它仍然抛出json解析错误。

    这是lambda处理程序代码的一部分:

    public class LambdaFunctionHandler implements RequestHandler<Object, String> {
    
        @Override
        public String handleRequest(Object input, Context context) {
            System.out.println("input - " + input);
            return "response";
        }
    }
    


    这是json,parsehub正在发送给lambda:

    {
        "run_token": "I have removed this",
        "status": "complete",
        "md5sum": "90dc9753513a248502414e8d5345a6de /phfiles/ty6qie7-ut5C.gz ",
        "custom_proxies": "",
        "data_ready": 1,
        "template_pages": {},
        "start_time": "2019-01-30T11:01:58",
        "owner_email": "I have removed this",
        "webhook": "https://api endpoint of lambda function",
        "is_empty": false,
        "project_token": "I have removed this",
        "end_time": "2019-01-30T11:02:19",
        "start_running_time": "2019-01-30T11:01:59",
        "options_json": "{"recoveryRules": "{}", "rotateIPs": false, "sendEmail": true, "allowPerfectSimulation": false, "ignoreDisabledElements": true, "webhook": "https://api endpoint of lambda function", "outputType": "csv", "customProxies": "", "preserveOrder": false, "startTemplate": "main_template", "allowReselection": false, "proxyDisableAdblock": false, "proxyCustomRotationHybrid": false, "maxWorkers": "0", "loadJs": true, "startUrl": "https://address of the website from which data is extracted", "startValue": "{}", "maxPages": "0", "proxyAllowInsecure": false}",
        "start_value": "{}",
        "start_template": "main_template",
        "pages": 2,
        "start_url": "https://address of the website from which data is extracted"
    }
    


    这是我的cloudwatch日志中的输出:

    Lambda invocation failed with status: 400. Lambda request id: eecd695e-61e7-47d9-bc27-04628c99e158
    Execution failed: Could not parse request body into json: Unrecognized token 'run_token': was expecting ('true', 'false' or 'null')
    at [Source: [B@36f6b2e9; line: 1, column: 11]
    


    这是我的eclipse控制台中的输出:

    Invoking function...
    ==================== INVOCATION ERROR ====================
    com.amazonaws.services.lambda.model.InvalidRequestContentException: Could not parse request body into json: Unexpected character ('r' (code 114)): was expecting comma to separate Object entries
    at [Source: [B@1ade7b2b; line: 15, column: 21] (Service: AWSLambda; Status Code: 400; Error Code: InvalidRequestContentException; Request ID: b46bf0b4-4bb2-4bc0-aa13-81457349153c)
    

    我们可以看到 “options戡json”:“{”恢复规则“{}”, ……部分json未转义。无法更改parsehub发送的json。我只能对lambda进行数据操作。

    0 回复  |  直到 6 年前