【问题标题】:curl POST statement to RCurl or httrcurl POST 语句到 RCurl 或 httr
【发布时间】:2014-10-28 14:46:47
【问题描述】:

我有这个工作 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 中实现这个属性?

【问题讨论】:

  • 尝试使用-v(详细标志)和httr::POST 使用verbose() 运行curl 并比较输出。这将帮助您找出请求之间的不同之处。
  • 看来我需要在 httr 中指定 --data-binary 选项。有没有办法做到这一点?
  • 您需要弄清楚该选项的作用
  • 当使用 --data 选项上传数据时,换行符和回车符被删除......这对我来说是个问题。使用 --data-binary 选项上传数据时,不会对文件进行任何处理。

标签: r libcurl rcurl httr


【解决方案1】:

我不是诺基亚开发人员,我假设这些不是您真正的 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

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

【讨论】:

【解决方案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() 读取数据后将数据作为字符串插入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多