RCurl getForm pass http headers

2019-04-11 18:44发布

问题:

Using RCurl's getForm function, which is the only nice way of passing in GET-parameters, I need to alter some http headers. In getURI, you just pass httpheader = c(Whatever='whatever',...) and it'll work. Unfortunately, that argument seems to be ignored by getForm.

How do I set the http headers in a getForm request?

回答1:

Welcome to the confusing world of RCurl! You've discovered that its syntax makes no sense, which is not your fault.

In getForm you pass headers as the second argument (the ...). See the usage section of ? getForm:

getForm(uri, ..., .params = character(), .opts = list(), curl = getCurlHandle(),
         .encoding = integer(), binary = NA, .checkParams = TRUE)

The arguments section says:

... the name-value pairs of parameters. Note that these are not the CURL options.

By contrast, the other workhorse function getURL says:

getURL(url, ..., .opts = list(),
        write = basicTextGatherer(.mapUnicode = .mapUnicode),
         curl = getCurlHandle(), async = length(url) > 1,
           .encoding = integer(), .mapUnicode = TRUE)

... named values that are interpreted as CURL options governing the HTTP request.

Thus, when using getForm, you can just pass the headers as a list, but when using getURL, you need to specify them in a httpheader argument.

My general advice is therefore to always use curlPerform instead of any of the wrapper functions (like getForm or getURL), because then you'll always be using a consistent syntax.



标签: r http curl get rcurl