3

I'm trying to access the US Census Geocoder batch address API, found here: http://geocoding.geo.census.gov/geocoder/

I've also gone through the documentation for the API here: http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf

I'm trying to use the httr package in R to post a batch csv file of formatted addresses using this format: Unique ID, Street address, City, State, ZIP I've tried the single address request version using getURL from RCurl and that works fine, but postForm doesn't seem to submit the file in the right way. The code I'm using right now seems to post the request correctly, but I'm not getting any geocoded data back.

curlhandle <- handle("http://geocoding.geo.census.gov/geocoder/geographies/addressbatch", 
  cookies = TRUE)

# using fileUpload from RCurl instead of upload_file from httr
upload3  <- fileUpload(contents = address100, contentType = "file")

test <- POST(url="http://geocoding.geo.census.gov/geocoder/geographies/addressbatch", 
  body = list(addressFile = upload3,
         benchmark = "Public_AR_Census2010",
         vintage="Census2010_Census2010"), 
  encode = "multipart",
  handle = curlhandle, 
  followLocation = TRUE,
  verbose = TRUE)

Is my request missing something? I'm not sure if I should be using writefunction & writedata in this case. Any help would be appreciated!

Sandy
  • 31
  • 1
  • 4
  • What error are you getting? What is `address100`? Why are you using `httr` but not `upload_file`? Try posting to http://httpbin.org/post to see if your request is OK. – Jeroen Ooms Nov 20 '14 at 20:28
  • Thanks! I was getting content(test) would be a single string that said "may not be null" which indicated the request went through but was bad somehow and so a null response was showing. I used RCurl's fileUpload because upload_file seems to only accept paths to files, while I was trying to upload an R data object. – Sandy Nov 22 '14 at 03:59

1 Answers1

3

You seem to have a weird mix of RCurl and httr. Here's how I'd write the request:

req <- POST(
  "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch", 
  body = list(
    addressFile = upload_file(address100),
    benchmark = "Public_AR_Census2010",
    vintage = "Census2010_Census2010"
  ), 
  encode = "multipart",
  verbose())
stop_for_status(req)
content(req)

(also "file" is not a valid mime type)

hadley
  • 102,019
  • 32
  • 183
  • 245
  • Great thanks! That seemed to work, except I had to replace address100 with a path to a saved csv file. I tried using upload_file with an object in R, which would return this error: Error: is.character(path) is not TRUE. Does upload_file only work with paths to files? The RCurl version fileUpload was what I was using before, which I thought would give the same output but accepts R data objects, but actually when I tried using that it didn't work. What are the differences between the two? As I understand it, upload_file is a wrapper for fileUpload. – Sandy Nov 22 '14 at 03:56
  • The whole point of `upload_file` is to _upload_ a _file_ that exists on disk. If it doesn't exist it won't work. – hadley Nov 22 '14 at 20:49