Skip to contents

Mocking writing to disk

Examples

# enable mocking
enable()
#> CrulAdapter enabled!
#> HttrAdapter enabled!
#> Httr2Adapter enabled!

# Write to a file before mocked request -------------

# crul
library(crul)
#> Warning: package ‘crul’ was built under R version 4.5.1
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(body = file(f))
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpqft8RC/file53044febe20.json
#>     response_headers: 
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
(out <- HttpClient$new("https://httpbin.org/get")$get(disk = f))
#> <crul response> 
#>   url: https://httpbin.org/get
#>   request_headers: 
#>     User-Agent: libcurl/8.5.0 r-curl/6.4.0 crul/1.5.0.91
#>     Accept-Encoding: gzip, deflate
#>     Accept: application/json, text/xml, application/xml, */*
#>   response_headers: 
#>   status: 200
out$content
#> [1] "/tmp/Rtmpqft8RC/file53044febe20.json"
#> attr(,"type")
#> [1] "file"
readLines(out$content)
#> [1] "{\"hello\":\"world\"}"
stub_registry_clear()

# httr
library(httr)
#> 
#> Attaching package: ‘httr’
#> The following object is masked from ‘package:crul’:
#> 
#>     handle
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = file(f),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpqft8RC/file530198a96a3.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
## with httr, you must set overwrite=TRUE or you'll get an errror
out <- GET("https://httpbin.org/get", write_disk(f, overwrite = TRUE))
out
#> Response [https://httpbin.org/get]
#>   Date: 2025-07-02 22:23
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 18 B
#> <ON DISK>  /tmp/Rtmpqft8RC/file530198a96a3.json
out$content
#> [1] "/tmp/Rtmpqft8RC/file530198a96a3.json"
#> attr(,"class")
#> [1] "path"
content(out, "text", encoding = "UTF-8")
#> [1] "{\"hello\":\"world\"}\n"
stub_registry_clear()

# httr2
library(httr2)
#> 
#> Attaching package: ‘httr2’
#> The following objects are masked from ‘package:crul’:
#> 
#>     url_build, url_parse
#> The following object is masked from ‘package:webmockr’:
#> 
#>     last_request
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = file(f),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpqft8RC/file5302b88b219.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
req <- request("https://httpbin.org/get")
out <- req_perform(req, path = f)
out
#> <httr2_response>
#> GET https://httpbin.org/get
#> Status: 200 OK
#> Content-Type: application/json
#> Body: On disk /tmp/Rtmpqft8RC/file5302b88b219.json (18 bytes)
out$body
#> [1] "/tmp/Rtmpqft8RC/file5302b88b219.json"
#> attr(,"class")
#> [1] "httr2_path"
out$headers
#> <httr2_headers>
#> content-type: application/json
readLines(out$body)
#> [1] "{\"hello\":\"world\"}"
stub_registry_clear()

# Use mock_file to have webmockr handle file and contents -------------

# crul
library(crul)
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(body = mock_file(f, "{\"hello\":\"mars\"}\n"))
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpqft8RC/file5302a8bef2a.json
#>     response_headers: 
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
(out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = f))
#> <crul response> 
#>   url: https://httpbin.org/get
#>   request_headers: 
#>     User-Agent: libcurl/8.5.0 r-curl/6.4.0 crul/1.5.0.91
#>     Accept-Encoding: gzip, deflate
#>     Accept: application/json, text/xml, application/xml, */*
#>   response_headers: 
#>   status: 200
out$content
#> [1] "/tmp/Rtmpqft8RC/file5302a8bef2a.json"
readLines(out$content)
#> [1] "{\"hello\":\"mars\"}" ""                    
stub_registry_clear()

# httr
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpqft8RC/file53045da2005.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
out <- GET("https://httpbin.org/get", write_disk(f))
out
#> Response [https://httpbin.org/get]
#>   Date: 2025-07-02 22:23
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 15 B
#> <ON DISK>  /tmp/Rtmpqft8RC/file53045da2005.json
## view stubbed file content
out$content
#> [1] "/tmp/Rtmpqft8RC/file53045da2005.json"
#> attr(,"class")
#> [1] "path"
readLines(out$content)
#> [1] "{\"foo\": \"bar\"}"
content(out, "text", encoding = "UTF-8")
#> [1] "{\"foo\": \"bar\"}\n"
stub_registry_clear()

# httr2
library(httr2)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpqft8RC/file5303f246415.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
req <- request("https://httpbin.org/get")
out <- req_perform(req, path = f)
out
#> <httr2_response>
#> GET https://httpbin.org/get
#> Status: 200 OK
#> Content-Type: application/json
#> Body: On disk /tmp/Rtmpqft8RC/file5303f246415.json (15 bytes)
## view stubbed file content
out$body
#> [1] "/tmp/Rtmpqft8RC/file5303f246415.json"
#> attr(,"class")
#> [1] "httr2_path"
readLines(out$body)
#> [1] "{\"foo\": \"bar\"}"
stub_registry_clear()

# disable mocking
disable()
#> CrulAdapter disabled!
#> HttrAdapter disabled!
#> Httr2Adapter disabled!