I have been using jsonlite to make REST GET calls. The number of parameters have increased and I am wondering how to make a REST POST call using R.
Per your request ...
library(jsonlite)
Instead of
fromJSON(myurl, ...)
you need to call httr::POST
directly:
txt <- httr::POST(myurl, ...)
fromJSON(txt)
The basic mechanism for using jsonlite::fromJSON
is to pass it a string. In the special case that you pass an URL (regexpr: ^https?://
), it does you a courtesy by calling httr::GET
and taking its output as your intended input. You can see this intent by looking at its source by typing in jsonlite::fromJSON
and finding the line with if(grepl("^https?://" ...
; if you try to find the function download_raw
, you'll not find immediately since it is an un-exported function. You can find it as jsonlite:::download_raw
(notice the third colon).
Looking at that function's source, you'll see that it makes direct calls to httr::GET
. You can mimic how download_raw
is calling httr::GET
, modifying the arguments as needed. (It might be informative to look at both help(httr::GET)
and help(httr::POST)
and look for the differences between them. Spoiler: look at the body
argument, potentially a list for keys/values. The examples are helpful.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.