在R中使用httr向API发布数据和从API接收数据 [英] Posting to and Receiving data from API using httr in R

查看:115
本文介绍了在R中使用httr向API发布数据和从API接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问美国人口普查地理编码器批处理地址API,可在此处找到: http://geocoding.geo.census.gov/geocoder/

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

我还在这里浏览了API的文档: http://geocoding .geo.census.gov/geocoder/Geocoding_Services_API.pdf

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

我正在尝试使用R中的httr包发布使用以下格式的格式化地址的批处理csv文件:唯一ID,街道地址,城市,州/省,邮政编码 我已经尝试过使用RCurl中的getURL使用单个地址请求版本,并且效果很好,但是postForm似乎没有以正确的方式提交文件.我现在正在使用的代码似乎可以正确地发布请求,但是我没有收到任何经过地理编码的数据.

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)

我的请求是否缺少某些内容?我不确定是否应该使用writefunction&在这种情况下为writedata.任何帮助将不胜感激!

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

推荐答案

您似乎混合使用了RCurl和httr.这是我编写请求的方式:

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)

("file"也是无效的mime类型)

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

这篇关于在R中使用httr向API发布数据和从API接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆