R编程&x2B;发出Https的POST Json请求
我需要使用R程序发出HTTPS POST请求,将JSON数据上传到Splunk中 我正在探索curl库,但在HTTPS上执行简单GET调用时出错。。这让我觉得可能是curl没有提供对HTTPS请求的支持 Rstudio:版本1.0.136 R:3.3.2 curl:version2.3 ============================================== 示例代码:R编程&x2B;发出Https的POST Json请求,json,r,curl,Json,R,Curl,我需要使用R程序发出HTTPS POST请求,将JSON数据上传到Splunk中 我正在探索curl库,但在HTTPS上执行简单GET调用时出错。。这让我觉得可能是curl没有提供对HTTPS请求的支持 Rstudio:版本1.0.136 R:3.3.2 curl:version2.3 ============================================== 示例代码: library(curl) request <- curl_fetch_memory("https:
library(curl)
request <- curl_fetch_memory("https://httpbin.org/get")
Error : Error in curl_fetch_memory("https://httpbin.org/get") :
Failure when receiving data from the peer
however, i the similar GET request for http endpoint works pretty fine.
i.e. request <- curl_fetch_memory("http://httpbin.org/get")
库(curl)
请求可以使用httr
库获取和发布API端点
library(httr)
httr::GET("https://httpbin.org/get")
# Response [https://httpbin.org/get]
# Date: 2017-06-27 05:24
# Status: 200
# Content-Type: application/json
# Size: 326 B
# {
# "args": {},
# "headers": {
# "Accept": "application/json, text/xml, application/xml, */*",
# "Accept-Encoding": "gzip, deflate",
# "Connection": "close",
# "Host": "httpbin.org",
# "User-Agent": "libcurl/7.51.0 r-curl/2.5 httr/1.2.1"
# },
# "origin": "45.126.44.231",
# ...
类似地,还有一个用于发布的httr::POST
函数
一个示例帖子如下所示
requestBody <- paste0('{
"foo" : [
{"id" : "myId",
"values" : {"a" : "b"}
}
]
}')
res <- httr::POST(url = url,
httr::add_headers('Content-Type' = 'application/json'),
httr::add_headers('Accept' = 'application/json'),
httr::add_headers('X-Application-Id' = appId),
httr::add_headers('X-Api-Key' = apiKey),
body = requestBody,
encode = "json")
requestBody终于能够通过https调用在R中工作了
据推测,影响https流量而不是http的是公司代理的问题
以下是我执行的步骤。。多亏了这篇文章:
a)在Rstudio中,转到“工具-->全局选项-->软件包”。取消选中“用户Internet Explorer库/http代理”选项
b) #加载httr库
图书馆(httr)
c) #设置代理配置
设置配置(使用代理(url=,端口=,用户名=,密码=))
d) #这是为了防止SSL警告
设置配置(配置(ssl\U verifypeer=0L))
现在我可以执行Https请求了,没有任何问题。我也尝试过这个打开。。但我如何能够点击http GET请求,即[httr::GET(“但不是https GET”。对于https GET,我收到错误:curl::curl\u fetch\u内存中的错误(url,handle=handle):已达到超时。它与代理设置有关吗?
a) in Rstudio, go to "Tools --> Global Options --> Packages " . Uncheck the option "User Internet Explorer library/proxy for http"
b) # load httr library
library(httr)
c) # set proxy configuration
set_config(use_proxy(url="<corporate proxy>",port=<port>, username="<username>", password="<ur pwd>"))
d) # this is to prevent SSL warning
set_config( config( ssl_verifypeer = 0L ) )