7 - HTTP error 400 when calling system_retrieve_file()? I'm using hook_feeds_after_save and the simpleHTMLdom library to migrate inline images for a feed import. After I detect that an image tag exists and the url is qualified/valid I build a stream wrapper for the image file and call system_retrieve_file.

  $dirPrefix = 'archive';
  $uri = file_build_uri($dirPrefix . '/' . $filepath);

  if (file_prepare_directory($uri, FILE_CREATE_DIRECTORY)) {
    $destination = $uri . '/' . $filename;
    system_retrieve_file($img_url, $destination, TRUE, FILE_EXISTS_REPLACE);
  }


However, I receive 400 errors on each image;
HTTP error 400 occurred when trying to fetch http://DOMAIN.COM/C8B195/assets/images/01 Grapes 3.jpg.

If I just plop this url into a browser I get a 200 and the image is displayed. Same when I test this in something like the Advanced Rest Client application.

Also, I'm developing this site in a local environment.

Anyone have any clue how to debug this or why I may be receiving a 400 response?
In my case, I needed to encode urls, but not how you may expect with PHP's urlencode or rawurlencode.
For example, I needed to convert http://DOMAIN.COM/C8B195/assets/images/01 Grapes 3.jpg to http://DOMAIN.COM/C8B195/assets/images/01%20Grapes%203.jpg. I'm not sure why system_retrieve_file or drupal_http_request had a problem with the spaces.

  $encodedPath = rawurlencode($path);
  $encodedPath = str_replace('%3A', ':', str_replace('%2F', '/', $encodedPath));