crypts
crypts

Reputation: 41

RCurl POST request with headers and JSON data

So essentially, I'm wanting to get the RCurl equivalent of the following curl call:

curl -H "AUTH-KEY: soanclCNdnLDcnlNc" -H "Content-Type: application/json" -X POST -d '{"documents":["http://localhost:3000/documents/2","http://localhost:3000/documents/4"]}' http://localhost:3000/documents/download?format=zip

I was managing to get something from this, but it was always larger than what the curl call produced and wasnt able to be uncompressed:, and can't for the life of me find out what it is.

x= list(items=c("http://localhost:3000/documents/2", "http://localhost:3000/documents4"))
headers <- list('AUTH-KEY' = "soanclCNdnLDcnlNc", 'Accept' = 'application/json', 'Content-Type' = 'application/json')
postForm("http://localhost:3000/documents/download?format=zip", .opts=list(postfields=toJSON(x), httpheader=headers))

Upvotes: 4

Views: 3981

Answers (1)

antonio
antonio

Reputation: 11120

While there are fancier packages nowadays, RCurl still does its honest job.

In general to remap in RCurl:

curl -H "AUTH-KEY: xxxx"  \
     -H "Content-Type: "application/x-www-form-urlencoded" \
     -d '{"key1": "value1","key2": "value2"}' \
     "https://httpbin.org/post"

(adjust your headers and fields accordingly)

You use:

hdr=c(Authorization="xxxx", `Content-Type`="application/x-www-form-urlencoded")
flds='{"key1": "value1","key2": "value2"}'
postForm("https://httpbin.org/post",
        .opts=list(httpheader=hdr, postfields=flds))

Note the backticks for Content-Type to escape the minus.

Upvotes: 3

Related Questions