json 基于Python示例使用R查询服务器

qyzbxkaa  于 2023-05-30  发布在  Python
关注(0)|答案(1)|浏览(143)

我正在尝试将以下Python脚本转换为R

import pandas as pd
import requests
import json
url = "https://telraam-api.net/v1/reports/traffic"
body = {
    "id":"9000001463",
    "time_start":"2022-10-01 00:00:00Z",
    "time_end":"2022-10-02 00:00:00Z",
    "level":"segments",
    "format":"per-hour"
}
headers = {
  'X-Api-Key': '***My_API-Key***'
}
payload = str(body)
response = requests.request("POST", url, headers=headers, data=payload)
json = response.json()
dataframe = pd.DataFrame(json['report'])
dataframe.to_csv('test.csv')

其中My_API-Key代表我无法通信的API密钥。
我使用了以下代码:

get_telraam_data <- function(id = "9000001463",
                       time_start = "2022-10-01 00:00:00Z",
                       time_end = "2022-10-02 00:00:00Z",
                       level = "segments",
                       format = "per-hour",
                       token = "***My_API-Key***") {
  GET(
    url = sprintf("https://telraam-api.net/v1"),
    query = list(
      id = id,
      time_start = time_start,
      time_end = time_end,
      level = level,
      format = format
    ),
    content_type_json(),
    add_headers("X-Api-Key" = token)
  ) -> res

  stop_for_status(res)

  content(res, as="text", encoding="UTF-8") %>%
    fromJSON(flatten=TRUE) %>%
    as_tibble() %>%
    readr::type_convert()
}

但是查询似乎不起作用。
我哪里出错了?bodyquery吗?

r8uurelv

r8uurelv1#

requests.request("POST", ...)是一个POST调用,而httr示例使用的是GET。您可以通过调用一些测试服务来比较请求/响应,例如http://httpbin.org/。或者只是在自己的机器上打开一个套接字,看看请求有什么不同。对于httr,应该这样做:

library(httr)

id         <- "9000001463"
time_start <- "2022-10-01 00:00:00Z"
time_end   <- "2022-10-02 00:00:00Z"
level      <- "segments"
format     <- "per-hour"
token      <- "***My_API-Key***"

api_endpoint <- "https://httpbin.org/post"
# or just run netcat on your own host to check the request:
# nc -l 1234
#api_endpoint <- "http://localhost:1234"

POST(
  url = api_endpoint,
  body = list(
    id = id,
    time_start = time_start,
    time_end = time_end,
    level = level,
    format = format
  ),
  encode = "json",
  content_type_json(),
  add_headers("X-Api-Key" = token)
)
#> Response [https://httpbin.org/post]
#>   Date: 2023-05-28 19:15
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 851 B
#> {
#>   "args": {}, 
#>   "data": "{\"id\":\"9000001463\",\"time_start\":\"2022-10-01 00:00:00Z\",\"t...
#>   "files": {}, 
#>   "form": {}, 
#>   "headers": {
#>     "Accept": "application/json, text/xml, application/xml, */*", 
#>     "Accept-Encoding": "deflate, gzip", 
#>     "Content-Length": "128", 
#>     "Content-Type": "application/json", 
#> ...

我个人使用httr2

library(dplyr)
library(httr2)
response <-  request(api_endpoint) %>% 
  req_headers("X-Api-Key" = token) %>% 
  req_body_json(list(
    id = id,
    time_start = time_start,
    time_end = time_end,
    level = level,
    format = format)) %>% 
  # to check request without actually sending it:
  # req_dry_run()
  req_perform(verbosity = 2) %>% 
  resp_body_json(simplifyVector = TRUE) 
#> -> POST /post HTTP/1.1
#> -> Host: httpbin.org
#> -> User-Agent: httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip
#> -> X-Api-Key: ***My_API-Key***
#> -> Content-Type: application/json
#> -> Content-Length: 128
#> -> 
#> >> {"id":"9000001463","time_start":"2022-10-01 00:00:00Z","time_end":"2022-10-02 00:00:00Z","level":"segments","format":"per-hour"}
#> <- HTTP/1.1 200 OK
#> <- Date: Sun, 28 May 2023 19:15:26 GMT
#> <- Content-Type: application/json
#> <- Content-Length: 807
#> <- Connection: keep-alive
#> <- Server: gunicorn/19.9.0
#> <- Access-Control-Allow-Origin: *
#> <- Access-Control-Allow-Credentials: true
#> <- 
#> << {
#> <<   "args": {}, 
#> <<   "data": "{\"id\":\"9000001463\",\"time_start\":\"2022-10-01 00:00:00Z\",\"time_end\":\"2022-10-02 00:00:00Z\",\"level\":\"segments\",\"format\":\"per-hour\"}", 
#> <<   "files": {}, 
#> <<   "form": {}, 
#> <<   "headers": {
#> <<     "Accept": "*/*", 
#> <<     "Accept-Encoding": "deflate, gzip", 
#> <<     "Content-Length": "128", 
#> <<     "Content-Type": "application/json", 
#> <<     "Host": "httpbin.org", 
#> <<     "User-Agent": "httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0", 
#> <<     "X-Amzn-Trace-Id": "Root=1-6473a84e-5ed51b7c52cac4ce2a19f855", 
#> <<     "X-Api-Key": "***My_API-Key***"
#> <<   }, 
#> <<   "json": {
#> <<     "format": "per-hour", 
#> <<     "id": "9000001463", 
#> <<     "level": "segments", 
#> <<     "time_end": "2022-10-02 00:00:00Z", 
#> <<     "time_start": "2022-10-01 00:00:00Z"
#> <<   }, 
#> <<   "origin": "90.191.104.119", 
#> <<   "url": "https://httpbin.org/post"
#> << }

创建于2023-05-28带有reprex v2.0.2

相关问题