Search code examples
rhttrqualtrics

Downloading Qualtrics user-submitted files from API response in R


probably a bit of a noob question but I've managed to get a successful response from the Qualtrics API using a url like this: https://yul1.qualtrics.com/API/v3/surveys/survey-id/responses/response-id/uploaded-files/file-id

My question is how to save the file that has been downloaded by the request to a given file path?

FYI response$headers$`content-disposition`[1] does return an accurate filename so am confident the call has worked correctly.

Thanks so much for any help!


Solution

  • If it's about {httr}, you can use httr::write_disk() to store response body in a file:

    httr::GET("https://raw.githubusercontent.com/r-lib/httr/main/tests/testthat/data.txt",
              httr::write_disk("data.txt"))
    #> Response [https://raw.githubusercontent.com/r-lib/httr/main/tests/testthat/data.txt]
    #>   Date: 2023-08-24 08:43
    #>   Status: 200
    #>   Content-Type: text/plain; charset=utf-8
    #>   Size: 12 B
    #> <ON DISK>  ~\AppData\Local\Temp\RtmpcdZrWa\reprex-454011914356-cilia-stag\data.txt
    fs::file_info("data.txt")[,1:3]
    #> # A tibble: 1 × 3
    #>   path       type         size
    #>   <fs::path> <fct> <fs::bytes>
    #> 1 data.txt   file           12
    

    Created on 2023-08-24 with reprex v2.0.2