将 HTTP 流传递到文件
使用 GET 方法向指定的 URL 发送 HTTP 请求,并将响应保存在 TMP 文件中。
缺省情况下, TMP 文件保存到 %localappdata%\Temp\ 文件夹。
| 术语 | 描述 |
|---|---|
| HTTP 请求 | 客户发出 HTTP 请求,访问或修改服务器上的资源。 |
| GET | GET 方法从特定资源请求数据。 使用 GET 方法的请求仅返回数据。 |
| URL | URL (统一资源定位器)是 URI(通用资源标识符)的一种特殊类型。 URL 通常定位互联网上的现有资源。 它用于标识目标服务器。 |
| TMP 文件 | 临时文件,通常具有 .tmp 扩展名。 |
语法
IBM RPA 的专有脚本语言的语法与其他编程语言类似。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。
httpStreamToFile --url(String) [--headers(StringDictionary<String>)] [--username(String)] [--password(String)] [--timeout(TimeSpan)] (Boolean)=success (String)=value (Numeric)=statusCode (String)=reasonPhrase (StringDictionary<String>)=headers (StringDictionary<String>)=contentHeaders
输入参数
下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。
| 设计器方式标签 | 脚本方式名称 | 必需的 | 接受的变量类型 | 描述 |
|---|---|---|---|---|
| URL | url |
Required |
Text |
提交请求的 URL。 |
| headers | headers |
Optional |
String Dictionary<Text> |
包含完成请求所需的信息的请求头。 必需的头字段通常记录在资源的 API 文档中。 |
| 用户名 | username |
Optional |
Text |
网络凭证用户。 用于代理认证的参数。 |
| 密码 | password |
Optional |
Text |
网络凭证密码。 用于代理认证的参数。 |
| Timeout | timeout |
Optional |
Time Span, Number, Text |
等待响应的最长时间。 缺省超时为 5 秒。 它还可以使用 Set Timeout (setTimeout) 命令定义的超时。 |
输出参数
| 设计器方式标签 | 脚本方式名称 | AcceptedTypes | 描述 |
|---|---|---|---|
| 成功 | success |
Boolean |
如果响应状态码为 2xx,那么返回 True 。 否则,返回 False。 |
| 文件 | value |
Text |
返回 .tmp 文件的完整路径。 |
| 状态代码 | statusCode |
Number |
返回 HTTP 请求状态代码。 请参阅 statusCode 参数选项 以获取详细信息。 |
| 原因阐述 | reasonPhrase |
Text |
返回状态码的简短文本描述。 |
| headers | headers |
String Dictionary<Text> |
返回请求的响应头。 |
| 内容头 | contentHeaders |
String Dictionary<Text> |
返回请求的响应内容头。 |
statusCode 参数选项
下表显示了对请求的可能状态码响应:
| 响应代码 | 描述 |
|---|---|
| 1xx | 信息响应。 |
| 2xx | 成功响应。 |
| 3xx | 重定向消息。 |
| 4xx | 请求错误。 |
| 5xx | 服务器上存在错误。 |
如果要了解更多信息,请参阅 状态码 🡥 以获取详细信息。
示例
示例 1: 下面的示例展示了如何使用 HTTP Stream to File 从 API 获取文件,在本例中是一个 gif 图像。 该脚本将获取桌面文件夹路径以在其中保存 gif。 然后,向 API 发出请求,创建临时文件。 最后,将文件重命名并粘贴到桌面文件夹上。
defVar --name success --type Boolean
defVar --name catFilePath --type String
defVar --name returnedStatusCode --type Numeric
defVar --name reasonPhrase --type String
defVar --name responseHeaders --type StringDictionary --innertype String
defVar --name responseContentHeaders --type StringDictionary --innertype String
defVar --name desktopFolderPath --type String
//--------------------------------About this script--------------------------------
//Description: This script gets an image and saves it to your Desktop folder
//Last updated: 02/01/2023
//-----------------------------------------------------------------------------------
getSpecialFolder --folder "Desktop" --comment "Gets the desktop folder path" desktopFolderPath=value
logMessage --message "Obtained desktop folder path: ${desktopFolderPath}" --type "Info"
httpStreamToFile --url "https://cataas.com/cat/gif" --comment "Make a GET HTTP request to https://cataas.com/cat/gif and save the response in a TEMP file " success=success returnedStatusCode=statusCode reasonPhrase=reasonPhrase responseHeaders=headers responseContentHeaders=contentHeaders catFilePath=value
logMessage --message "Request made to \"https://cataas.com/cat/gif\". It returns:\r\nSuccess: ${success}\r\nStatus code: ${returnedStatusCode}\r\nReason phrase: ${reasonPhrase}\r\nResponse headers: ${responseHeaders}\r\nResponse content headers: ${responseContentHeaders}\r\nPath to the temp file: ${catFilePath}" --type "Info"
fileRename --file "${catFilePath}" --newname "cat.gif" --comment "Renaming and setting the correct extension to the file" catFilePath=value
logMessage --message "Temp file renamed to cat.gif" --type "Info"
fileMove --from "${catFilePath}" --to "${desktopFolderPath}" --comment "Move the file to the desktop"
logMessage --message "File moved to your descktop: ${catFilePath}" --type "Info"
例 2: 下面的示例展示了如何使用 HTTP Stream to File 从任何 URL 获取文件,本例中的命令文档为 HTML。 此脚本会获取桌面文件夹路径以将文档保存在此目录中。 然后,向 URL 发送请求,创建临时文件。 最后,将文件重命名并保存在桌面文件夹中。
defVar --name success --type Boolean
defVar --name httpStreamToFileFilePath --type String
defVar --name returnedStatusCode --type Numeric
defVar --name reasonPhrase --type String
defVar --name responseHeaders --type StringDictionary --innertype String
defVar --name responseContentHeaders --type StringDictionary --innertype String
defVar --name desktopFolderPath --type String
//--------------------------------About this script--------------------------------
//Description: This script gets an HTML file from IBM Docs
//Last updated: 02/01/2023
//-----------------------------------------------------------------------------------
getSpecialFolder --folder "Desktop" --comment "Gets the desktop folder path" desktopFolderPath=value
logMessage --message "Obtained desktop folder path: ${desktopFolderPath}" --type "Info"
httpStreamToFile --url "https://www.ibm.com/docs/en/rpa/23.0?topic=web-http-stream-file" --comment "Make a GET HTTP request to https://www.ibm.com/docs/en/rpa/23.0?topic=web-http-stream-file and save the response in a TEMP file " success=success returnedStatusCode=statusCode reasonPhrase=reasonPhrase responseHeaders=headers responseContentHeaders=contentHeaders httpStreamToFileFilePath=value
logMessage --message "Request made to \"https://www.ibm.com/docs/en/rpa/23.0?topic=web-http-stream-file\". It returns:\r\nSuccess: ${success}\r\nStatus code: ${returnedStatusCode}\r\nReason phrase: ${reasonPhrase}\r\nResponse headers: ${responseHeaders}\r\nResponse content headers: ${responseContentHeaders}\r\nPath to the temp file: ${httpStreamToFileFilePath}" --type "Info"
fileRename --file "${httpStreamToFileFilePath}" --newname "HTTP Stream to File.html" --comment "Renaming and setting the correct extension to the file" httpStreamToFileFilePath=value
logMessage --message "Temp file renamed to HTTP Stream to File.html" --type "Info"
fileMove --from "${httpStreamToFileFilePath}" --to "${desktopFolderPath}" --comment "Move the file to the desktop"
logMessage --message "File moved to your descktop: ${httpStreamToFileFilePath}" --type "Info"