π http_request
π http_request
Make HTTP requests and save the response body to a file. Inherits from file.
β‘ Actions
| Action | Description |
|---|---|
:get |
HTTP GET request (default) |
:post |
HTTP POST request |
:put |
HTTP PUT request |
:delete |
HTTP DELETE request |
:options |
HTTP OPTIONS request |
:nothing |
Do nothing (use with notifications) |
π Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
url |
String | required β οΈ | URL to request |
path |
String | Resource name | Destination file for the response body |
headers |
Hash | {} |
HTTP request headers |
message |
String | "" |
Request body (for POST/PUT) |
redirect_limit |
Integer | 10 |
Maximum number of redirects to follow |
mode |
String | β | File permissions for saved response |
owner |
String | β | File owner |
group |
String | β | File group |
sensitive |
Boolean | false |
Hide content diff in output π |
π How It Works
- π Fetch β Makes the HTTP request, following redirects up to
redirect_limit - π₯ Save β Sets response body as the file content
- π Compare β Diffs response content against the existing file
- π Write β Saves to the destination path (using the
fileresourceβs write logic)
Error Handling
| HTTP Status | Behavior |
|---|---|
| 2xx β | Success β saves response body |
| 3xx β©οΈ | Follows redirect (up to redirect_limit) |
| 4xx β | Raises HTTPClientError |
| 5xx π₯ | Raises HTTPServerError |
π‘ HTTPS is supported β SSL is automatically enabled for
https://URLs.
π¬ Dry-Run Behavior
β οΈ Important: The HTTP request is actually made during
pre_action(to fetch the response body for diff comparison). The content is compared but not written to the target path. Be aware of this side effect.
π Examples
Download a file
http_request '/tmp/archive.tar.gz' do
url 'https://example.com/releases/v1.0.tar.gz'
end
POST with headers
http_request '/tmp/api_response.json' do
action :post
url 'https://api.example.com/deploy'
headers(
'Authorization' => 'Bearer token123',
'Content-Type' => 'application/json'
)
message '{"environment": "production"}'
end
Download with permissions
http_request '/usr/local/bin/tool' do
url 'https://releases.example.com/tool-linux-amd64'
mode '0755'
owner 'root'
end
Download with redirect following
http_request '/tmp/latest-release.tar.gz' do
url 'https://github.com/user/repo/releases/latest/download/app.tar.gz'
redirect_limit 5
end
𧬠Inheritance
Inherits from file β all file attributes (mode, owner, group, sensitive) are available.
β οΈ Note: The standard
fileactions (:create,:delete,:edit) are replaced by HTTP verb actions. You cannot use:createor:editon anhttp_requestresource.