gpt4 book ai didi

r - 将 curl 命令转换为 Rcurl

转载 作者:行者123 更新时间:2023-12-04 16:08:56 27 4
gpt4 key购买 nike

如何转换此命令:

curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'

curl 命令在 Rcurl 中?

最佳答案

curlconverter的dev版(devtools::install_github("hrbrmstr/curlconverter")可以转换curl命令行字符串和认证和详细的参数:

将您的 URL 复制到剪贴板:

curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'

然后运行:

library(curlconverter)
req <- make_req(straighten())[[1]]

以下内容现在将出现在您的剪贴板中:

httr::VERB(verb = "GET", url = "https://domain.freshdesk.com/api/v2/tickets", 
httr::authenticate(user = "abcdefghij1234567890",
password = "X"), httr::verbose(),
httr::add_headers(), encode = "json")

req 现在也是一个可调用函数。你可以这样做:

req
## function ()
## httr::VERB(verb = "GET", url = "https://domain.freshdesk.com/api/v2/tickets",
## httr::authenticate(user = "abcdefghij1234567890", password = "X"),
## httr::verbose(), httr::add_headers(), encode = "json")

或者通过实际调用它:

req()

我通常会重新格式化函数源以使其更具可读性:

httr::VERB(verb = "GET", 
url = "https://domain.freshdesk.com/api/v2/tickets",
httr::authenticate(user = "abcdefghij1234567890", password = "X"),
httr::verbose(),
httr::add_headers(),
encode = "json")

您可以轻松地将其转换为没有命名空间的普通 GET 调用:

GET(url = "https://domain.freshdesk.com/api/v2/tickets", 
authenticate(user = "abcdefghij1234567890", password = "X"),
verbose(),
add_headers(),
encode = "json"))

我们可以通过在您的示例中进行小替换来验证它是否可以使用经过身份验证的 curl 命令行:

curl_string <- 'curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET "https://httpbin.org/basic-auth/abcdefghij1234567890/X"'

make_req(straighten(curl_string))[[1]]()
## -> GET /basic-auth/abcdefghij1234567890/X HTTP/1.1
## -> Host: httpbin.org
## -> Authorization: Basic YWJjZGVmZ2hpajEyMzQ1Njc4OTA6WA==
## -> User-Agent: libcurl/7.43.0 r-curl/1.2 httr/1.2.1
## -> Accept-Encoding: gzip, deflate
## -> Accept: application/json, text/xml, application/xml, */*
## ->
## <- HTTP/1.1 200 OK
## <- Server: nginx
## <- Date: Tue, 30 Aug 2016 14:13:12 GMT
## <- Content-Type: application/json
## <- Content-Length: 63
## <- Connection: keep-alive
## <- Access-Control-Allow-Origin: *
## <- Access-Control-Allow-Credentials: true
## <-
## Response [https://httpbin.org/basic-auth/abcdefghij1234567890/X]
## Date: 2016-08-30 14:13
## Status: 200
## Content-Type: application/json
## Size: 63 B
## {
## "authenticated": true,
## "user": "abcdefghij1234567890"
## }

关于r - 将 curl 命令转换为 Rcurl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39220915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com