Post Request Using Rcurl

POST request using RCurl

With httr, this is just:

library(httr)
r <- POST("http://www.datasciencetoolkit.org/text2people",
body = "Tim O'Reilly, Archbishop Huxley")
stop_for_status(r)
content(r, "parsed", "application/json")

Send a POST request using httr R package

Try

library(httr)
login <- list(
email = "login",
password = "password",
submit = "Login!"
)
res <- POST("http://kenpom.com/handlers/login_handler.php", body = login, encode = "form", verbose())
team <- GET("http://kenpom.com/team.php?team=Rice", verbose())

How to make a POST request with header and data options in R using httr::POST?

You could try this; with content type and headers added:

link <- "http://www.my-api.com"
df <- list(name="Fred", age="5")

httr::POST(url = link,
body = jsonlite::toJSON(df, pretty = T, auto_unbox = T),
httr::add_headers(`accept` = 'application/json'),
httr::content_type('application/json'))

How to get httr POST request to work in R?

You are missing order and you need to explicitly convert your request to json. Give it a try:

library("RCurl")
library("rjson")

# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

h = basicTextGatherer()
hdr = basicHeaderGatherer()

req = list(
order = list(instrument="EUR_USD",
units="1",
side="buy",
type="market")
)

body = enc2utf8(toJSON(req))
Token = "abc123" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', Token, sep=' ')

h$reset()
curlPerform(url = "",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)

headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))

# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(headers)
}

print("Result:")
result = h$value()
print(fromJSON(result))

My guess is that it is automatically conveted into a text, and there is no matching handler method for text

Making a GET request in R

Try out the new and further improving curlconverter package. It will take a curl request and output an httr command.

#devtools::install_github("hrbrmstr/curlconverter")

library(curlconverter)

curlExample <- "curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'"

resp <- make_req(straighten(curlExample))
resp

curl -X POST command with RCurl

You are specifying the userpwd argument incorrectly. Try:

postForm('https://api.example.com/resource.xml',
From='value',
To='value',
Body='value',
.opts=list(userpwd="username:password"))

Note: RCurl syntax is kind of idiosyncratic, so in postForm the ... argument refers to HTTP headers whereas in getURL (from your previous question), the ... argument refers to curl options. This is probably what is tripping you up.



Related Topics



Leave a reply



Submit