You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Power Automate流中HTTP请求无法等待API响应的问题求助

Power Automate流中HTTP请求无法等待API响应的问题求助

我最近在折腾Power Automate流的时候快愁死了!本来是做一个从发票里提取采购订单的流程——触发条件是收件箱收到新邮件,然后遍历PDF附件,调用AI Builder提取信息。后来为了让采购订单的提取更准确,我把这部分逻辑移到了自己开发的API里,现在流只需要把PDF发给这个API端点,剩下的处理用C#完成后再返回结果就行。

这个API在Postman里调用大概20秒就能返回结果,但一放到Power Automate的HTTP请求里就直接超时。我给API挂了调试器后发现,刚好在我等待AI客户端响应的那个节点,Flow居然重新发送了请求,直接搞成了无限循环!不过我测试过,如果在AI调用之前就返回OK响应,流是能正常工作的,说明端点的调用本身是没问题的。

我现在满脑子疑问:怎么才能让Power Automate的HTTP请求老老实实等这个响应?还是说问题出在AI客户端调用的异步特性上?调试的时候我还发现,第一次调用aiClient(也就是请求里第一个异步操作)的时候,断点总是莫名其妙丢失。

Power Automate中HTTP操作的截图

下面是Power Automate里HTTP调用的代码视图:

"inputs": {
    "method": "POST",
    "uri": "REDACTED",
    "headers": {
        "Connection": "keep-alive",
        "Accept": "*/*",
        "Host": "REDACTED",
        "Accept-Encoding": "gzip, deflate, br"
    },
    "body": {
        "$content-type": "multipart/form-data",
        "$multipart": [
            {
                "headers": {
                    "Content-Disposition": "form-data; name=\"invoicePdf\"; filename=\"invoicePdf.pdf\""
                },
                "body": "@outputs('Get_file_content')"
            }
        ]
    },
    "authentication": {
        "type": "Basic",
        "username": "REDACTED",
        "password": "REDACTED"
    }
},
"operationOptions": "DisableAsyncPattern",
"metadata": {
    "operationMetadataId": "38dae010-f79b-4c3e-ae7c-22a6047b4151"
}

这是我写的包含AI调用逻辑的C#方法:

{
    // Check if a file was uploaded
    if (invoicePdf == null || invoicePdf.Length == 0)
    {
        return BadRequest("No file uploaded.");
    }

    // Check if the file is a PDF
    if (!invoicePdf.FileName.EndsWith(".pdf"))
    {
        return BadRequest("Only PDF files are allowed.");
    }

    AzureKeyCredential credential = new AzureKeyCredential(AppSettings.AzureAiServiceKey);
    DocumentAnalysisClient aiClient = new DocumentAnalysisClient(new Uri(AppSettings.AzureAiServiceEndpoint), credential);

    //var operation = await aiClient.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", invoicePdf.OpenReadStream());
    var invoiceOperation = aiClient.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice",
        invoicePdf.OpenReadStream(), new AnalyzeDocumentOptions
        {
            Pages = { "1-2" }
        }).Result;

    var invoiceResult = invoiceOperation.Value;

    //check default area
    for (int i = 0; i < invoiceResult.Documents.Count; i++)
    {
        var document = invoiceResult.Documents[i];

        //try to find the purchase order from the default area
        if (document.Fields.ContainsKey("PurchaseOrder"))
        {
            document.Fields.TryGetValue("PurchaseOrder", out var purchaseOrderObject);

            var purchaseOrderValue = purchaseOrderObject.Content;
            var purchaseOrderConfidence = purchaseOrderObject.Confidence;

            var sanitizedPurchaseOrderValue = SanitizePurchaseOrder(purchaseOrderValue);

            return Ok(new
            {
                Found = true,
                PurchaseOrderValue = sanitizedPurchaseOrderValue,
                PurchaseOrderConfidence = purchaseOrderConfidence,
                FoundIn = "Default Location"
            });
        }
    }

    var layoutOperation = aiClient.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-document",
        invoicePdf.OpenReadStream(), new AnalyzeDocumentOptions
        {
            Pages = { "1-2" }
        }).Result;

    var layoutResult = layoutOperation.Value;
    DocumentKeyValuePair? poKeyValuePair;

    poKeyValuePair = layoutResult.KeyValuePairs.FirstOrDefault(x => firstPriorityKeys.Contains(x.Key.Content));
    if (poKeyValuePair == null)
    {
        poKeyValuePair = layoutResult.KeyValuePairs.FirstOrDefault(x => secondPriorityKeys.Contains(x.Key.Content));
        if (poKeyValuePair == null)
        {
            poKeyValuePair = layoutResult.KeyValuePairs.FirstOrDefault(x => thirdPriorityKeys.Contains(x.Key.Content));
            if (poKeyValuePair == null)
            {
                poKeyValuePair = layoutResult.KeyValuePairs.FirstOrDefault(x => fourthPriorityKeys.Contains(x.Key.Content));
                if (poKeyValuePair == null)
                {
                    poKeyValuePair = layoutResult.KeyValuePairs.FirstOrDefault(x => fifthPriorityKeys.Contains(x.Key.Content));
                }
            }
        }
    }

    if (poKeyValuePair != null)
    {
        var sanitizedPurchaseOrderValue = SanitizePurchaseOrder(poKeyValuePair.Value.Content);

        return Ok(new
        {
            Found = true,
            PurchaseOrderValue = sanitizedPurchaseOrderValue,
            PurchaseOrderConfidence = poKeyValuePair.Confidence,
            FoundIn = $"{poKeyValuePair.Key.Content} (KVP)"
        });
    }

    //check plain text with regex
    Regex regex =
        new Regex(@"([Pp][Oo]|[Pp][Uu][Rr][Cc][Hh][Aa][Ss][Ee]\s?[Oo][Rr][Dd][Ee][Rr]).{0,3}(\d{7})");
    Match match = regex.Match(layoutResult.Content);
    if (match.Success)
    {
        var foundPo = match.Value;
        var sanitizedPurchaseOrderValue = SanitizePurchaseOrder(foundPo);

        return Ok(new
        {
            Found = true,
            PurchaseOrderValue = sanitizedPurchaseOrderValue,
            PurchaseOrderConfidence = 0.5,
            FoundIn = "Plain Text Regex Parse"
        });

    }

    return Ok(new
    {
        Found = false
    });
}

问题核心总结

  • Power Automate流的HTTP请求不等待API完成处理,直接超时并重发请求,导致无限循环
  • API在Postman中能正常返回(约20秒响应),但在Flow中触发异常
  • 想明确:是能配置Flow让HTTP请求等待完整响应,还是问题出在AI客户端的异步调用逻辑上?

备注:内容来源于stack exchange,提问作者impo

火山引擎 最新活动