写一个复杂的curl请求的R等价物。

问题描述 投票:3回答:1

我有一个curl请求。

curl -k --insecure -X POST https://somehttp -H 'Content-Type: application/x-www-form-urlencoded' -H 'cache-control: no-cache' -d 'grant_type=password&client_id=my_id&username=admin&password=admin&client_secret=k6897pyy-1h7l-11q0-lp10-s20sg4erlq44' | jq .access_token

我怎么能用R写这个请求呢? 我看过一些简单的例子,但没有这么复杂。

r curl httr rcurl
1个回答
0
投票

显然,我不能用你提供的例子来测试,但这应该能让你接近。首先确保你正确填充这些变量。

url       <- "http://www.somehttp.com"
my_id     <- "my_id"
username  <- "admin"
password  <- "admin"
my_secret <- "k6897pyy-1h7l-11q0-lp10-s20sg4erlq4"

现在下面的代码准备请求,发送请求,并解析响应(我假设它是json)。

library(httr)

H <- add_headers(`Content-Type`= "application/x-www-form-urlencoded",
                  `cache-control` = "no-cache")

form_body <- list(grant_type    = "password", 
                  client_id     = my_id,
                  username      = username, 
                  password      = password,
                  client_secret = my_secret)

res <- POST(url = url, body = form_body, encode = "form", H)

content(res, "parsed")
© www.soinside.com 2019 - 2024. All rights reserved.