在 Azure 逻辑应用中处理内容类型Handle content types in Azure Logic Apps

有各种类型的内容可以流过逻辑应用,例如 JSON、XML、平面文件和二进制数据。Various content types can flow through a logic app, for example, JSON, XML, flat files, and binary data. 逻辑应用支持所有内容类型,并且原生支持其中的某些类型,而不需要在逻辑应用中进行强制转换或转换。While Logic Apps supports all content types, some have native support and don't require casting or conversion in your logic apps. 但是,还有一些类型可能需要进行强制转换或转换。Other types might require casting or conversion as necessary. 本文介绍逻辑应用如何处理内容类型,以及我们在必要时如何正确强制转换或转换这些类型。This article describes how Logic Apps handles content types and how you can correctly cast or convert these types when necessary.

逻辑应用依赖于 HTTP 调用中的 Content-Type 标头值来确定处理内容类型的适当方式,例如:To determine the appropriate way for handling content types, Logic Apps relies on the Content-Type header value in HTTP calls, for example:

application/jsonapplication/json

逻辑应用会存储并处理使用 application/json 内容类型作为 JavaScript 表示法 (JSON) 对象的任何请求。Logic Apps stores and handles any request with the application/json content type as a JavaScript Notation (JSON) object. 默认情况下,无需任何强制转换,即可分析 JSON 内容。By default, you can parse JSON content without any casting. 若要分析包含内容类型为“application/json”的标头的请求,可以使用表达式。To parse a request that has a header with the "application/json" content type, you can use an expression. 此示例返回 animal-type 数组中的 dog 值,且无需强制转换:This example returns the value dog from the animal-type array without casting:

@body('myAction')['animal-type'][0]

{
  "client": {
     "name": "Fido",
     "animal-type": [ "dog", "cat", "rabbit", "snake" ]
  }
}

如果处理的 JSON 数据未指定标头,可以使用 json() 函数手动将该数据强制转换为 JSON,例如:If you're working with JSON data that doesn't specify a header, you can manually cast that data to JSON by using the json() function, for example:

@json(triggerBody())['animal-type']

创建 JSON 属性的标记Create tokens for JSON properties

逻辑应用提供相应的功能来生成用户友好的标记,用于表示 JSON 内容中的属性,以便在逻辑应用的工作流中更轻松地引用和使用这些属性。Logic Apps provides the capability for you to generate user-friendly tokens that represent the properties in JSON content so you can reference and use those properties more easily in your logic app's workflow.

  • 请求触发器Request trigger

    在逻辑应用设计器中使用此触发器时,可以提供一个 JSON 架构用于描述预期要接收的有效负载。When you use this trigger in the Logic App Designer, you can provide a JSON schema that describes the payload you expect to receive. 设计器使用此架构分析 JSON 内容,并生成用户友好的标记来表示 JSON 内容中的属性。The designer parses JSON content by using this schema and generates user-friendly tokens that represent the properties in your JSON content. 然后,你可以在整个逻辑应用工作流中轻松引用和使用这些属性。You can then easily reference and use those properties throughout your logic app's workflow.

    如果没有架构,可以生成架构。If you don't have a schema, you can generate the schema.

    1. 在请求触发器中,选择“使用示例有效负载生成架构”。 In the Request trigger, select Use sample payload to generate schema.

    2. 在“输入或粘贴示例 JSON 有效负载”下,提供示例有效负载并选择“完成”。 Under Enter or paste a sample JSON payload, provide a sample payload and then choose Done. 例如:For example:

      提供示例 JSON 有效负载

      此时,生成的架构会显示在触发器中。The generated schema now appears in your trigger.

      提供示例 JSON 有效负载

      下面是代码视图编辑器中请求触发器的基础定义:Here is the underlying definition for your Request trigger in the code view editor:

      "triggers": { 
         "manual": {
            "type": "Request",
            "kind": "Http",
            "inputs": { 
               "schema": {
                  "type": "object",
                  "properties": {
                     "client": {
                        "type": "object",
                        "properties": {
                           "animal-type": {
                              "type": "array",
                              "items": {
                                 "type": "string"
                              },
                           },
                           "name": {
                              "type": "string"
                           }
                        }
                     }
                  }
               }
            }
         }
      }
      
    3. 在请求中,请务必包含 Content-Type 标头,并将标头值设置为 application/jsonIn your request, make sure you include a Content-Type header and set the header's value to application/json.

  • Parse JSON 操作Parse JSON action

    在逻辑应用设计器中使用此操作时,可以分析 JSON 输出,并生成用户友好的标记用于表示 JSON 内容中的属性。When you use this action in the Logic App Designer, you can parse JSON output and generate user-friendly tokens that represent the properties in your JSON content. 然后,你可以在整个逻辑应用工作流中轻松引用和使用这些属性。You can then easily reference and use those properties throughout your logic app's workflow. 与请求触发器类似,可以提供或生成一个 JSON 架构用于描述想要分析的 JSON 内容。Similar to the Request trigger, you can provide or generate a JSON schema that describes the JSON content you want to parse. 这样,便可以更轻松地使用 Azure 服务总线、Azure Cosmos DB 等服务中的数据。That way, you can more easily consume data from Azure Service Bus, Azure Cosmos DB, and so on.

    分析 JSON

text/plaintext/plain

当逻辑应用收到 Content-Type 标头设置为 text/plain 的 HTTP 消息时,逻辑应用将以原始格式存储这些消息。When your logic app receives HTTP messages that have the Content-Type header set to text/plain, your logic app stores those messages in raw form. 如果在后续操作中包含这些消息且不进行强制转换,则会在将 Content-Type 标头设置为 text/plain 的情况下传出请求。If you include these messages in subsequent actions without casting, requests go out with the Content-Type header set to text/plain.

例如,在处理某个平面文件时,可能会收到 Content-Type 标头设置为 text/plain 内容类型的 HTTP 请求:For example, when you're working with a flat file, you might get an HTTP request with the Content-Type header set to text/plain content type:

Date,Name,Address
Oct-1,Frank,123 Ave

如果随后在某个后续操作中发送此请求作为另一个请求的正文(例如 @body('flatfile')),则第二个请求的 Content-Type 标头也会设置为 text/plainIf you then send this request on in a later action as the body for another request, for example, @body('flatfile'), that second request also has a Content-Type header that's set to text/plain. 如果正在处理纯文本数据但未指定标头,可按以下表达式中所示,使用 string() 函数手动将该数据强制转换为文本:If you're working with data that is plain text but didn't specify a header, you can manually cast that data to text by using the string() function such as this expression:

@string(triggerBody())

application/xml and application/octet-streamapplication/xml and application/octet-stream

逻辑应用始终保留收到的 HTTP 请求或响应中的 Content-TypeLogic Apps always preserves the Content-Type in a received HTTP request or response. 因此,如果逻辑应用收到了 Content-Type 设置为 application/octet-stream 的内容,而你在某个后续操作中包含该内容且不进行强制转换,则传出的请求的 Content-Type 也会设置为 application/octet-streamSo if your logic app receives content with Content-Type set to application/octet-stream, and you include that content in a later action without casting, the outgoing request also has Content-Type set to application/octet-stream. 这样,逻辑应用可以保证在经历工作流的不同阶段时,该数据不会丢失。That way, Logic Apps can guarantee that data doesn't get lost while moving through the workflow. 但是,当状态在工作流中转移时,操作状态或输入和输出将存储在 JSON 对象中。However, the action state, or inputs and outputs, is stored in a JSON object while the state moves through the workflow.

转换器函数Converter functions

为了保留某些数据类型,逻辑应用会将内容转换为二进制 base64 编码的字符串,该字符串包含同时保留 $content 有效负载和 $content-type(可自动转换)的相应元数据。To preserve some data types, Logic Apps converts content to a binary base64-encoded string with appropriate metadata that preserves both the $content payload and the $content-type, which are automatically converted.

此列表描述了使用这些函数时逻辑应用如何转换内容:This list describes how Logic Apps converts content when you use these functions:

  • json():将数据强制转换为 application/jsonjson(): Casts data to application/json
  • xml():将数据强制转换为 application/xmlxml(): Casts data to application/xml
  • binary():将数据强制转换为 application/octet-streambinary(): Casts data to application/octet-stream
  • string():将数据强制转换为 text/plainstring(): Casts data to text/plain
  • base64():将内容转换为 base64 编码的字符串base64(): Converts content to a base64-encoded string
  • base64toString():将 base64 编码的字符串转换为 text/plainbase64toString(): Converts a base64-encoded string to text/plain
  • base64toBinary():将 base64 编码的字符串转换为 application/octet-streambase64toBinary(): Converts a base64-encoded string to application/octet-stream
  • dataUri():将字符串转换为数据 URIdataUri(): Converts a string to a data URI
  • dataUriToBinary():将数据 URI 转换为二进制字符串dataUriToBinary(): Converts a data URI to a binary string
  • dataUriToString():将数据 URI 转换为字符串dataUriToString(): Converts a data URI to a string

例如,如果收到 Content-Type 设置为 application/xml 的 HTTP 请求,如以下内容所示:For example, if you receive an HTTP request where Content-Type set to application/xml, such as this content:

<?xml version="1.0" encoding="UTF-8" ?>
<CustomerName>Frank</CustomerName>

可以使用包含 xml()triggerBody() 函数的 @xml(triggerBody()) 表达式来强制转换此内容,然后再使用此内容。You can cast this content by using the @xml(triggerBody()) expression with the xml() and triggerBody() functions and then use this content later. 或者,可以使用包含 xpath()xml() 函数的 @xpath(xml(triggerBody()), '/CustomerName') 表达式。Or, you can use the @xpath(xml(triggerBody()), '/CustomerName') expression with the xpath() and xml() functions.

其他内容类型Other content types

逻辑应用使用并支持其他内容类型,但你可能需要通过解码 $content 变量来手动获取消息正文。Logic Apps works with and supports other content types, but might require that you manually get the message body by decoding the $content variable.

例如,假设具有 application/x-www-url-formencoded 内容类型的请求触发了逻辑应用。For example, suppose your logic app gets triggered by a request with the application/x-www-url-formencoded content type. 为了保留所有数据,请求正文中的 $content 变量包含一个编码为 base64 字符串的有效负载:To preserve all the data, the $content variable in the request body has a payload that's encoded as a base64 string:

CustomerName=Frank&Address=123+Avenue

由于该请求未采用纯文本或 JSON 格式,因此将存储在操作中,如下所示:Because the request isn't plain text or JSON, the request is stored in the action as follows:

"body": {
    "$content-type": "application/x-www-url-formencoded",
    "$content": "AAB1241BACDFA=="
}

逻辑应用提供本机函数用于处理表单数据:Logic Apps provides native functions for handling form data, for example:

或者,可以使用以下示例所示的表达式来手动访问数据:Or, you can manually access the data by using an expression such as this example:

@string(body('formdataAction'))

如果希望传出的请求包含相同的 application/x-www-url-formencoded 内容类型标头,可将请求添加到操作的正文,而无需使用类似于 @body('formdataAction') 的表达式执行任何强制转换。If you wanted the outgoing request to have the same application/x-www-url-formencoded content type header, you can add the request to the action's body without any casting by using an expression such as @body('formdataAction'). 但是,仅当正文是 body 输入中的唯一参数时,此方法才有效。However, this method only works when the body is the only parameter in the body input. 如果尝试在 application/json 请求中使用 @body('formdataAction') 表达式,将发生运行时错误,因为发送了编码的正文。If you try to use the @body('formdataAction') expression in an application/json request, you get a runtime error because the body is sent encoded.