I am trying to download images from a url list that was downloaded from the kobotoolbox server. When i am trying to run the below script, it is showing me "Error in curl::curl_fetch_memory(url, handle = handle) : URL using bad/illegal format or missing URL".
I checked the url and it is functional. Btw the username and password given is not the actual one. Can anyone help?
username <- "XX"
password <- "XX"
image_url <- "https://kc-eu.kobotoolbox.org/media/original?media_file=rasidul01%2Fattachments%2F87b1508d4ab84daea15a434e49588df0%2F1c74f4b2-02e0-4c38-b45c-fd02f1d64260%2F1709790982589.jpg
response <- GET(image_url, authenticate(username, password))
Error in curl::curl_fetch_memory(url, handle = handle) :
URL using bad/illegal format or missing URL
Answers
The issue with your code seems to be related to the URL string. It appears that the URL is not properly formatted, likely due to the special characters in the query parameters. Specifically, the %
characters in the URL should be URL-encoded.
You can use the URLencode()
function in R to properly encode the URL before using it in your request. Here's how you can modify your code to fix the issue:
library(httr)
username <- "XX"
password <- "XX"
image_url <- "https://kc-eu.kobotoolbox.org/media/original?media_file=rasidul01%2Fattachments%2F87b1508d4ab84daea15a434e49588df0%2F1c74f4b2-02e0-4c38-b45c-fd02f1d64260%2F1709790982589.jpg"
# Encode the URL
encoded_url <- URLencode(image_url)
# Send the request
response <- GET(encoded_url, authenticate(username, password))
By encoding the URL using URLencode()
, you ensure that any special characters in the URL are properly encoded, making it a valid URL format that can be used in your HTTP request. This should resolve the "bad/illegal format" error you're encountering.