Home > database >  How can I get in to an API content that looks like this?
How can I get in to an API content that looks like this?

Time:11-11

The API specifications


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'

How can I read it in httr in R? or with another package?

Thanks!!

CodePudding user response:

It helps to have a known api endpoint where you can compare results; from Getting started with httr, I'll use 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"
}

In R, I can replicate that (mostly) with:

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"
# }

Similarly,

$ 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"
}

and

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"
# }
  • Related