0

I have this working curl statement to post a file to Nokia's HERE batch geocoding service...

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'

I have tried this:

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"))

And this:

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

Using this file as example.txt: https://gist.github.com/corynissen/4f30378f11a5e51ad9ad

Is there any way to do this property in R?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
cory
  • 6,529
  • 3
  • 21
  • 41
  • Try running curl with `-v` (verbose flag) and `httr::POST` with `verbose()` and comparing the output. That will help you figure out what is different between the requests. – hadley Oct 28 '14 at 22:16
  • It looks like I need to specify the --data-binary option in httr. Is there a way to do that? – cory Oct 29 '14 at 13:03
  • You need to figure out _what that option does_. – hadley Oct 29 '14 at 14:29
  • When data is uploaded using the --data option, newlines and carriage returns are stripped out... which is a problem for me. When data is uploaded using the --data-binary option, no processing is done on the file. – cory Oct 29 '14 at 15:29

2 Answers2

3

I'm not a Nokia developer, and I'm assuming those are not your real API creds. This should help you get further with 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

But, I can't be sure w/o registering (and no time to do that).

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • This gets me really close. I get a 400 bad request error. I think what I'm missing is the requirement to submit the payload using the --data-binary parameter. Page 20 of the Nokia HERE batch geocoder doc... 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 Oct 28 '14 at 18:01
-2

This is the solution based on hrbrmstr's solution 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)

The problem I had to get around was that the normal upload process strips line breaks... but I needed them in there for the API to work (--data-binary option in curl does this). To get around this, I insert the data as a string after reading it via readLines().

cory
  • 6,529
  • 3
  • 21
  • 41