创建 HTTP 内容

创建用于存储一条或多条 HTTP 消息的内容和头的变量。

命令可用性: 本地 IBM RPA SaaS 和 IBM RPA

描述

HTTP (超文本传输协议) 是在客户机与服务器之间建立通信的协议。 请参阅 HTTP 请求 以获取详细信息。

语法

IBM RPA 具有与其他编程语言相似的专有脚本语言。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 "脚本" 方式下使用此语法。

createHttpContent --formattype(Nullable<HttpFormatter>) [--boundary(String)] [--contentheader(String)] [--body(String)] [--parameters(String)] [--encoding(Nullable<EncodingType>)] [--mediatype(String)] --file(String) [--contentlist(String)] (HttpContent)=value

输入参数

下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。

设计器方式标签 脚本方式名称 必需的 接受的变量类型 描述
格式设置器 formattype Required HttpFormatter 文本格式。 请参阅 formattype 参数选项 以获取详细信息。
边界 boundary Optional Text 用于分隔不同 HTTP 消息的文本。
内容头 contentheader Optional Text 包含来自请求的信息的内容头。
正文 body Optional Text HTTP 请求主体的内容。 主体参数 是引用相同值的输入。
参数 parameters Optional Text 应在请求正文中发送的参数。 ParametersBody 是引用相同值的输入。
编码 encoding Optional EncodingType 要用于请求的文本编码格式。 请参阅 encoding 参数选项 以获取详细信息。
媒体类型 mediatype Optional Text 请求主体的格式。 例如,text/plain
文件 file Only when Formatter is Bytes Text 要发送的文件。
内容 contentlist Optional Text 要使用的 HTTP 消息的映射。 在 参数 字段中,输入要映射的元素。 在 字段中,输入用于接收映射元素的内容的变量。

formattype 参数选项

下表显示可用于 formattype 参数的选项。 该表显示了在脚本方式下工作时的可用选项以及在设计器方式下的等效标签。

设计器方式标签 脚本方式名称
字节数 Bytes
表单 URL 编码 FormUrlEncoded
JSON Json
多重部件 Multipart
文本 Text
XML Xml

encoding 参数选项

下表显示可用于 encoding 参数选项的选项。 该表显示了在脚本方式下工作时的可用选项以及在设计器方式下的等效标签。

设计器方式标签 脚本方式名称
ASCII ASCII
大尾数法 Unicode Big Endian Unicode
操作系统缺省值 Default
Unicode Unicode
UTF32 UTF32
UTF7 UTF7
UTF8 UTF8

输出参数

Designer 方式 脚本方式 接受的变量类型 描述
内容 value Http Content 返回 HTTP 请求的内容。

示例

以下代码示例创建不同的 HTTP 消息,将所有这些消息存储在一个变量中,并执行 POST 方法请求,使不同的消息即时执行这些请求。

defVar --name content --type HttpContent
defVar --name multicontentvar --type HttpContent
defVar --name content2 --type HttpContent
defVar --name content1 --type HttpContent
defVar --name content3 --type HttpContent
defVar --name content4 --type HttpContent
defVar --name success --type Boolean
defVar --name response --type String
createHttpContent --formattype "Json" --contentheader "MyHeader1: test port:8080" --body "{\"tests\":\r\n    {\r\n        \"test1\":\"First\",\r\n        \"test2\":\"Second\"\r\n    }\r\n}" content=value
createHttpContent --formattype "Json" --contentheader "MyHeader2: test port:8080" --parameters "test1=First,test2=Second" content1=value
createHttpContent --formattype "Text" --contentheader "MyHeader3: test port:80" --body "<html>\r\n<body>\r\n</body>\r\n</html>" --encoding "Default" --mediatype "application/html" content2=value
createHttpContent --formattype "Xml" --contentheader "MyHeader5: test port:8080" --body "<note>\r\n<from>Anonymous</from>\r\n<to>You</to>\r\n<messsage>We need more coffee.</message>\r\n</note>\r\n" content3=value
createHttpContent --formattype "Xml" --contentheader "MyHeader6: test port:8080" --body "\r\n" --parameters "<from>=Anonymous,<to>=You,<messsage>=We need more coffee." content4=value
createHttpContent --formattype "Multipart" --boundary asdfasf --contentheader "Warning: test warning" --contentlist "content1=${content},content2=${content1},content3=${content2},content4=${content3},content5=${content4}" multicontentvar=value
httpRequest --verb "Post" --url "https://httpbin.org/post" --formatter "Instance" --source "${multicontentvar}" --noproxy  success=success response=value
logMessage --message "Success: ${success}\r\nResponse: ${response}" --type "Info"