timcdlucas
timcdlucas

Reputation: 1364

GET from terminal works but httr::GET does not

I'm trying to download rasters from a server with httr::GET. We used to have this working but some changes have been made to the server and now it does not work.

Using the URL either through the browser or through GET in the terminal (ubuntu 16.04) works fine and returns a working tif raster. But using the same url from httr::GET does not work and gives Status: 400.

My only guesses are it's something to do with the encoding of the data. But I'm really not sure.

file <- paste0(tempdir(), '/file.tif')
r <- 
  httr::GET('https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time(\"2015-01-01T00:00:00.000Z\")',
           httr::write_disk(file, overwrite = TRUE))

ras <- raster::raster(file)

# I'm now totally confused about when and where quotes are escaped, so just to make sure...
r <- 
  httr::GET('https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")',
            httr::write_disk(file, overwrite = TRUE))

ras <- raster::raster(file)

# But just putting the url in the browser works fine.
# https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")

# eg 
# rr <- raster::raster('~/Desktop/2015_Nature_Africa_PR3.tif')


# And using the URL with GET in the terminal works
# GET "https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time(\"2015-01-01T00:00:00.000Z\")" > ~/Desktop/2015_Nature_Africa_PR4.tif
# eg
# rr <- raster::raster('~/Desktop/2015_Nature_Africa_PR4.tif')

Upvotes: 2

Views: 135

Answers (1)

neilfws
neilfws

Reputation: 33772

Wrapping in URLEncode worked for me:

library(httr)
library(raster)

file <- paste0(tempdir(), '/file.tif')
url1 <- 'https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")'

r   <- GET(URLencode(url1), write_disk(file, overwrite = TRUE))
ras <- raster(file)

Upvotes: 5

Related Questions