0

我有这个工作 curl 语句可以将文件发布到诺基亚的 HERE 批量地理编码服务......

curl -X POST -H 'Content-Type: multipart/form-data;boundary=----------------------------4ebf00fbcf09' \
     --data-binary @example.txt \
     'http://batch.geocoder.cit.api.here.com/6.2/jobs?action=run&mailto=test@gmail.com&maxresults=1&language=es-ES&header=true&indelim=|&outdelim=|&outcols=displayLatitude,displayLongitude,houseNumber,street,district,city,postalCode,county,state,country,matchLevel,relevance&outputCombined=false&app_code=AJKnXv84fjrb0KIHawS0Tg&app_id=DemoAppId01082013GAL'

我试过这个:

library(RCurl) 
url <- "http://batch.geocoder.cit.api.here.com/6.2/jobs?    action=run&mailto=test@gmail.com&maxresults=1&language=es-ES&header=true&indelim=|&outdelim=|&outcols=displayLatitude,displayLongitude,houseNumber,street,district,city,postalCode,county,state,country,matchLevel,relevance&outputCombined=false&app_code=AJKnXv84fjrb0KIHawS0Tg&app_id=DemoAppId01082013GAL'" 
postForm(url, file=fileUpload(filename="example.txt",
                 contentType="multipart/form-data;boundary=----------------------------4ebf00fbcf09"))

和这个:

library(httr)
a <- POST(url, body=upload_file("example.txt", type="text/plain"),
          config=c(add_headers("multipart/form-data;boundary=----------------------------4ebf00fbcf09")))
content(a)

将此文件用作example.txthttps ://gist.github.com/corynissen/4f30378f11a5e51ad9ad

有没有办法在 R 中做这个属性?

4

2 回答 2

3

我不是诺基亚开发人员,我假设这些不是您真正的 API 信誉。这应该可以帮助您进一步了解httr

url <- "http://batch.geocoder.cit.api.here.com/6.2/jobs"

a <- POST(url, encode="multipart",                      # this will set the header for you
          body=list(file=upload_file("example.txt")),   # this is how to upload files
          query=list(
            action="run",
            mailto="test@example.com",
            maxresults="1",
            language="es-ES",                           # this will build the query string
            header="true",
            indelim="|",
            outdelim="|",
            outcols="displayLatitude,displayLongitude", # i shortened this for the example
            outputCombined="false",
            app_code="APPCODE",
            app_id="APPID"), 
          verbose())                                    # this lets you verify what's going on

但是,我不能确定没有注册(也没有时间这样做)。

于 2014-10-28T15:27:42.050 回答
-2

这是基于hrbrmstr的解决方案的解决方案 bod <- paste(readLines("example.txt", warn=F), collapse="\n") a <- POST(url, encode="multipart", # this will set the header for you body=bod, # this is how to upload files query=list( action="run", mailto="test@gmail.com", maxresults="1", language="es-ES", # this will build the query string header="true", indelim="|", outdelim="|", outcols="displayLatitude,displayLongitude,houseNumber,street,district,city,postalCode,county,state,country,matchLevel,relevance", # i shortened this for the example outputCombined="false", app_code="AJKnXv84fjrb0KIHawS0Tg", app_id="DemoAppId01082013GAL"), #config=c(add_headers("multipart/form-data;boundary=----------------------------4ebf00fbcf09")), verbose()) # this lets you verify what's going on content(a)

我必须解决的问题是正常的上传过程会去除换行符......但我需要它们在那里才能使 API 工作(curl 中的 --data-binary 选项可以做到这一点)。为了解决这个问题,我在通过 readLines() 读取数据后将数据作为字符串插入。

于 2014-10-29T19:16:14.153 回答