Install packages httr and jsonlite
#install.packages("httr")
#install.packages("jsonlite")
Create a GET response to call the API
foodbank <- httr::GET("https://www.givefood.org.uk/api/2/foodbanks/")
# View API GET response result
head(str(foodbank))
## List of 10
## $ url : chr "https://www.givefood.org.uk/api/2/foodbanks/"
## $ status_code: int 200
## $ headers :List of 24
## ..$ date : chr "Sun, 01 Oct 2023 17:15:24 GMT"
## ..$ content-type : chr "application/json"
## ..$ transfer-encoding : chr "chunked"
## ..$ connection : chr "keep-alive"
## ..$ vary : chr "Accept-Encoding"
## ..$ vary : chr "Cookie"
## ..$ access-control-allow-origin: chr "*"
## ..$ expires : chr "Mon, 02 Oct 2023 02:54:45 GMT"
## ..$ cache-control : chr "max-age=43200"
## ..$ content-security-policy : chr "img-src 'self' data: s.ytimg.com *.googleusercontent.com *.gstatic.com www.google-analytics.com www.googletagma"| __truncated__
## ..$ strict-transport-security : chr "max-age=2592000; includeSubDomains"
## ..$ x-content-type-options : chr "nosniff"
## ..$ x-xss-protection : chr "1; mode=block"
## ..$ referrer-policy : chr "same-origin"
## ..$ x-cloud-trace-context : chr "7adb93f69f5209b55d987d1fe043b717"
## ..$ age : chr "7388"
## ..$ cf-cache-status : chr "HIT"
## ..$ last-modified : chr "Sun, 01 Oct 2023 15:12:16 GMT"
## ..$ report-to : chr "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=RrRXpSVve%2F6bJgY6NcC4au0LzTa5QRGyK"| __truncated__
## ..$ nel : chr "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}"
## ..$ server : chr "cloudflare"
## ..$ cf-ray : chr "80f658f30b599933-ARN"
## ..$ content-encoding : chr "gzip"
## ..$ alt-svc : chr "h3=\":443\"; ma=86400"
## ..- attr(*, "class")= chr [1:2] "insensitive" "list"
## $ all_headers:List of 1
## ..$ :List of 3
## .. ..$ status : int 200
## .. ..$ version: chr "HTTP/1.1"
## .. ..$ headers:List of 24
## .. .. ..$ date : chr "Sun, 01 Oct 2023 17:15:24 GMT"
## .. .. ..$ content-type : chr "application/json"
## .. .. ..$ transfer-encoding : chr "chunked"
## .. .. ..$ connection : chr "keep-alive"
## .. .. ..$ vary : chr "Accept-Encoding"
## .. .. ..$ vary : chr "Cookie"
## .. .. ..$ access-control-allow-origin: chr "*"
## .. .. ..$ expires : chr "Mon, 02 Oct 2023 02:54:45 GMT"
## .. .. ..$ cache-control : chr "max-age=43200"
## .. .. ..$ content-security-policy : chr "img-src 'self' data: s.ytimg.com *.googleusercontent.com *.gstatic.com www.google-analytics.com www.googletagma"| __truncated__
## .. .. ..$ strict-transport-security : chr "max-age=2592000; includeSubDomains"
## .. .. ..$ x-content-type-options : chr "nosniff"
## .. .. ..$ x-xss-protection : chr "1; mode=block"
## .. .. ..$ referrer-policy : chr "same-origin"
## .. .. ..$ x-cloud-trace-context : chr "7adb93f69f5209b55d987d1fe043b717"
## .. .. ..$ age : chr "7388"
## .. .. ..$ cf-cache-status : chr "HIT"
## .. .. ..$ last-modified : chr "Sun, 01 Oct 2023 15:12:16 GMT"
## .. .. ..$ report-to : chr "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=RrRXpSVve%2F6bJgY6NcC4au0LzTa5QRGyK"| __truncated__
## .. .. ..$ nel : chr "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}"
## .. .. ..$ server : chr "cloudflare"
## .. .. ..$ cf-ray : chr "80f658f30b599933-ARN"
## .. .. ..$ content-encoding : chr "gzip"
## .. .. ..$ alt-svc : chr "h3=\":443\"; ma=86400"
## .. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
## $ cookies :'data.frame': 0 obs. of 7 variables:
## ..$ domain : logi(0)
## ..$ flag : logi(0)
## ..$ path : logi(0)
## ..$ secure : logi(0)
## ..$ expiration: 'POSIXct' num(0)
## ..$ name : logi(0)
## ..$ value : logi(0)
## $ content : raw [1:1171856] 5b 0a 20 20 ...
## $ date : POSIXct[1:1], format: "2023-10-01 17:15:24"
## $ times : Named num [1:6] 0 0.00477 0.07033 0.20139 0.30814 ...
## ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
## $ request :List of 7
## ..$ method : chr "GET"
## ..$ url : chr "https://www.givefood.org.uk/api/2/foodbanks/"
## ..$ headers : Named chr "application/json, text/xml, application/xml, */*"
## .. ..- attr(*, "names")= chr "Accept"
## ..$ fields : NULL
## ..$ options :List of 2
## .. ..$ useragent: chr "libcurl/7.84.0 r-curl/5.0.0 httr/1.4.5"
## .. ..$ httpget : logi TRUE
## ..$ auth_token: NULL
## ..$ output : list()
## .. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
## ..- attr(*, "class")= chr "request"
## $ handle :Class 'curl_handle' <externalptr>
## - attr(*, "class")= chr "response"
## NULL
# View the main source of the data needed from the API - Normally content
str(foodbank$content)
## raw [1:1171856] 5b 0a 20 20 ...
Convert the raw content to text (in this case, a JSON string)
foodbankcontent <- httr::content(foodbank, as = "text")
## No encoding supplied: defaulting to UTF-8.
# View JSON String result
str(foodbankcontent)
## chr "[\n {\n \"name\": \"Lions Food Hub\",\n \"alt_name\": null,\n \"slug\": \"lions-food-hub\",\n \"ph"| __truncated__
Convert the JSON string to a dataframe and view data in a table
foodbankJSON <- jsonlite::fromJSON(foodbankcontent)
View(foodbankJSON)