I'm using the R package httr2
to upload files to a secure environment. If a file already exists, I'd like to receive a warning. Instead, I get a response status of 200 which, means "OK". However, the file is not updated on the server.
This is the code I use for the upload:
url <- "<server URL><Filepath on server>?action=upload"
req <- request(url) %>%
req_method("POST") %>%
req_headers("X-Auth-Token" = token) %>%
req_body_multipart(uploadFile = curl::form_file(file_path))
resp <- req_perform(req, verbosity=3)
I get the following status information:
> resp_status(resp)
[1] 200
> resp_status_desc(resp)
[1] "OK"
However, the curl status message (obtained using verbosity=3
with req_perform
) shows "code":20012,"type":"WARNING"}
. But the curl status is not included in the resp
object so I can't include a check that the upload went as intended.
If I do the same request in httr
instead of httr2
, then the status will also be 200 but I can use the content
function to obtain the curl status.
response <- PUT(
url,
add_headers(`X-Auth-Token` = token),
body = list(uploadFile = upload_file(file_path))
)
> response$status
[1] 200
> content(response)$status$code
[1] 20012
I am aware that I can append &overwrite=true
to the URL but I don't want to automatically overwrite the file. It's my understanding that the PUT
method is intended for existing files and that POST
should be used for new files. However, the server (LSAF 5.4.2, a statistical computing environment by SAS) only accepts PUT
.
Is there a way to programmatically extract the curl status code using httr2
?
try
/tryCatch
/if
-else
-approach."code":20012,"type":"WARNING"}
is obtained from the verbosity you could use sink to capture the console-output and thentryCatch
or handle it as you want. Other than that maybehttr2::resp_body_string(resp)
offers more detail.resp_body_json()
to get the response body.