什么是 Python comamnd requests.post() 的 R 对应物



用户可能知道R,但需要运行以下python代码

import requests
import json
query = "asthma"
r = requests.post("https://ndar.nih.gov/api/search/nda_sw_removal/collection/full", query)
collections = json.loads(r.text)
collections

使用 R 库,如何在 R 中重写 request.post 调用?挑战在于python究竟如何处理query参数。

我猜你正在寻找类似httr的东西:

从这里取一个例子:

b2 <- "http://httpbin.org/post"
POST(b2, body = "A simple text string")

另一个例子从这里取:

r <- POST("http://httpbin.org/post", body = list(a = 1, b = 2, c = 3))

根据Slam在评论中的建议:

另一个参考来源:http://httr.r-lib.org/reference/POST.html

library(httr)
query = "asthma"
r = POST("https://ndar.nih.gov/api/search/nda_sw_removal/collection/full", body=query)
library(jsonlite)
aa<-fromJSON(content(r,'text'))
str(aa)
View(aa$collection$results)