我正在尝试将以下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()
}
但是查询似乎不起作用。
我哪里出错了?body
是query
吗?
1条答案
按热度按时间r8uurelv1#
requests.request("POST", ...)
是一个POST
调用,而httr
示例使用的是GET
。您可以通过调用一些测试服务来比较请求/响应,例如http://httpbin.org/。或者只是在自己的机器上打开一个套接字,看看请求有什么不同。对于httr
,应该这样做:我个人使用
httr2
:创建于2023-05-28带有reprex v2.0.2