我怎样才能进入看起来像这样的 API 内容?
How can I get in to an API content that looks like this?
API规格
curl --location --request POST 'https://api.com/api/login/adm' \
--header 'Content-Type: application/json' \
--header 'Cookie: XXXXXXXXX' \
--data-raw '{
"usuario" : "XXX",
"clave" : "XXXXXXXXXXX"
}'
curl --location --request GET 'https://api-.com/api/XX/XX' \
--header 'Authorization: Bearer XXX_XXXX' \
--header 'Cookie: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
如何在 R 中的 httr 中读取它?还是用另一个包?
谢谢!!
拥有一个可以比较结果的已知 api 端点会很有帮助;从 Getting started with httr,我将使用 httpbin.org。
$ curl --location --request GET 'https://httpbin.org/get' \
--header 'Authorization: Bearer XXX_XXXX'
--header 'Cookie: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
{
"args": {},
"headers": {
"Accept": "*/*",
"Authorization": "Bearer XXX_XXXX",
"Cookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
"X-Amzn-Trace-Id": "Root=1-618be7c3-5c0267582e0de42f05e4d7cc"
},
"origin": "172.254.236.28",
"url": "https://httpbin.org/get"
}
在 R 中,我可以(主要)复制它:
library(httr)
library(magrittr) # for %>%, not strictly required, just for demo/vis
GET("https://httpbin.org/get", add_headers(Authorization = "Bearer XXX_XXXX"), set_cookies("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")) %>%
content(as="text") %>%
cat(., "\n")
# No encoding supplied: defaulting to UTF-8.
# {
# "args": {},
# "headers": {
# "Accept": "application/json, text/xml, application/xml, */*",
# "Accept-Encoding": "deflate, gzip",
# "Authorization": "Bearer XXX_XXXX",
# "Cookie": "=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
# "Host": "httpbin.org",
# "User-Agent": "libcurl/7.64.1 r-curl/4.3 httr/1.4.1",
# "X-Amzn-Trace-Id": "Root=1-618be835-2cab359e06d9bb525f209b8d"
# },
# "origin": "172.254.236.28",
# "url": "https://httpbin.org/get"
# }
同样,
$ curl --location --request POST 'https://httpbin.org/post' \
> --header 'Content-Type: application/json' \
> --header 'Cookie: XXXXXXXXX' \
> --data-raw '{
> "usuario" : "XXX",
> "clave" : "XXXXXXXXXXX"
> }'
{
"args": {},
"data": "{\n \"usuario\" : \"XXX\",\n \"clave\" : \"XXXXXXXXXXX\"\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "54",
"Content-Type": "application/json",
"Cookie": "XXXXXXXXX",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
"X-Amzn-Trace-Id": "Root=1-618be8de-2f1275a05b257ae7245120ae"
},
"json": {
"clave": "XXXXXXXXXXX",
"usuario": "XXX"
},
"origin": "172.254.236.28",
"url": "https://httpbin.org/post"
}
和
POST("https://httpbin.org/post", set_cookies("XXXXXXXXX"), body = list(usuario="XXX", clave="XXXXXXXXXXX"), encode="json") %>%
content(as="text") %>%
cat(., "\n")
# No encoding supplied: defaulting to UTF-8.
# {
# "args": {},
# "data": "{\"usuario\":\"XXX\",\"clave\":\"XXXXXXXXXXX\"}",
# "files": {},
# "form": {},
# "headers": {
# "Accept": "application/json, text/xml, application/xml, */*",
# "Accept-Encoding": "deflate, gzip",
# "Content-Length": "39",
# "Content-Type": "application/json",
# "Cookie": "=XXXXXXXXX",
# "Host": "httpbin.org",
# "User-Agent": "libcurl/7.64.1 r-curl/4.3 httr/1.4.1",
# "X-Amzn-Trace-Id": "Root=1-618be94c-20fb2500728a22f43d6a32db"
# },
# "json": {
# "clave": "XXXXXXXXXXX",
# "usuario": "XXX"
# },
# "origin": "172.254.236.28",
# "url": "https://httpbin.org/post"
# }
API规格
curl --location --request POST 'https://api.com/api/login/adm' \
--header 'Content-Type: application/json' \
--header 'Cookie: XXXXXXXXX' \
--data-raw '{
"usuario" : "XXX",
"clave" : "XXXXXXXXXXX"
}'
curl --location --request GET 'https://api-.com/api/XX/XX' \
--header 'Authorization: Bearer XXX_XXXX' \
--header 'Cookie: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
如何在 R 中的 httr 中读取它?还是用另一个包?
谢谢!!
拥有一个可以比较结果的已知 api 端点会很有帮助;从 Getting started with httr,我将使用 httpbin.org。
$ curl --location --request GET 'https://httpbin.org/get' \
--header 'Authorization: Bearer XXX_XXXX'
--header 'Cookie: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
{
"args": {},
"headers": {
"Accept": "*/*",
"Authorization": "Bearer XXX_XXXX",
"Cookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
"X-Amzn-Trace-Id": "Root=1-618be7c3-5c0267582e0de42f05e4d7cc"
},
"origin": "172.254.236.28",
"url": "https://httpbin.org/get"
}
在 R 中,我可以(主要)复制它:
library(httr)
library(magrittr) # for %>%, not strictly required, just for demo/vis
GET("https://httpbin.org/get", add_headers(Authorization = "Bearer XXX_XXXX"), set_cookies("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")) %>%
content(as="text") %>%
cat(., "\n")
# No encoding supplied: defaulting to UTF-8.
# {
# "args": {},
# "headers": {
# "Accept": "application/json, text/xml, application/xml, */*",
# "Accept-Encoding": "deflate, gzip",
# "Authorization": "Bearer XXX_XXXX",
# "Cookie": "=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
# "Host": "httpbin.org",
# "User-Agent": "libcurl/7.64.1 r-curl/4.3 httr/1.4.1",
# "X-Amzn-Trace-Id": "Root=1-618be835-2cab359e06d9bb525f209b8d"
# },
# "origin": "172.254.236.28",
# "url": "https://httpbin.org/get"
# }
同样,
$ curl --location --request POST 'https://httpbin.org/post' \
> --header 'Content-Type: application/json' \
> --header 'Cookie: XXXXXXXXX' \
> --data-raw '{
> "usuario" : "XXX",
> "clave" : "XXXXXXXXXXX"
> }'
{
"args": {},
"data": "{\n \"usuario\" : \"XXX\",\n \"clave\" : \"XXXXXXXXXXX\"\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "54",
"Content-Type": "application/json",
"Cookie": "XXXXXXXXX",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
"X-Amzn-Trace-Id": "Root=1-618be8de-2f1275a05b257ae7245120ae"
},
"json": {
"clave": "XXXXXXXXXXX",
"usuario": "XXX"
},
"origin": "172.254.236.28",
"url": "https://httpbin.org/post"
}
和
POST("https://httpbin.org/post", set_cookies("XXXXXXXXX"), body = list(usuario="XXX", clave="XXXXXXXXXXX"), encode="json") %>%
content(as="text") %>%
cat(., "\n")
# No encoding supplied: defaulting to UTF-8.
# {
# "args": {},
# "data": "{\"usuario\":\"XXX\",\"clave\":\"XXXXXXXXXXX\"}",
# "files": {},
# "form": {},
# "headers": {
# "Accept": "application/json, text/xml, application/xml, */*",
# "Accept-Encoding": "deflate, gzip",
# "Content-Length": "39",
# "Content-Type": "application/json",
# "Cookie": "=XXXXXXXXX",
# "Host": "httpbin.org",
# "User-Agent": "libcurl/7.64.1 r-curl/4.3 httr/1.4.1",
# "X-Amzn-Trace-Id": "Root=1-618be94c-20fb2500728a22f43d6a32db"
# },
# "json": {
# "clave": "XXXXXXXXXXX",
# "usuario": "XXX"
# },
# "origin": "172.254.236.28",
# "url": "https://httpbin.org/post"
# }