2014-10-28 8 views
0

나는이 시도컬 POST의 RCurl에 문 또는 HTTR

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&[email protected]&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&[email protected]&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.txt 등 : https://gist.github.com/corynissen/4f30378f11a5e51ad9ad

R에서이 속성을 수행 할 수있는 방법이 있습니까?

+0

'-v' (자세한 플래그) 및'verbose()'를 사용하여'httr :: POST'를 사용하여 curl을 실행하고 출력을 비교해보십시오. 그러면 요청간에 다른 점을 파악하는 데 도움이됩니다. – hadley

+0

httr에 --data-binary 옵션을 지정해야하는 것처럼 보입니다. 그렇게 할 수있는 방법이 있습니까? – cory

+0

그 옵션이하는 것이 무엇인지 알아 내야합니다. – hadley

답변

2

저는 Nokia 개발자가 아니며 실제 API 등급이 맞지 않다고 가정합니다. 등록하지 (그리고 시간하는 것을 할) O/승 나는 확신 할 수 없다,

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="[email protected]", 
      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 

을하지만 : 이것은 당신이 httr로 더 얻을 수 있도록해야한다. (

+0

정말 가까이에 있습니다. 400 개의 잘못된 요청 오류가 발생합니다. 내가 누락 된 부분은 --data-binary 매개 변수를 사용하여 페이로드를 제출해야한다는 것입니다. 노키아 여기에 배치 지오 코더 문서 20 ... http://developer.here.com/documentation/download/batch_geocoding_nlp/6.2.25.1/Batch%20Geocoder%20API%20v6.2.25.1%20Developer's%20Guide.pdf – cory

-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="[email protected]", 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()를 통해 데이터를 읽은 후 데이터를 문자열로 삽입합니다.