python - 带标题的 R httr 帖子

标签 python r httr

我在 Python 中有以下请求。我如何在 Rhttr 包中重写它?

我不确定的是如何翻译 R 中的 jsontuple 等价物。

import requests

cookies = {}

headers = {
    'Host': 'mysejahtera.malaysia.gov.my',
    'Accept': 'application/json',
    'Connection': 'keep-alive',
    'Content-Length': '77',
    'Authorization': 'Basic N0ZFRkRCMTMtN0Q2MC00NEQxLUE5MTctM0',
    'Content-Type': 'application/json',
    'Accept-Language': 'en-MY;q=1, ms-MY;q=0.9',
    'User-Agent': 'MySejahtera/1.0.36 (iPhone; iOS 14.6; Scale/2.00)',
}

params = (
    ('type', 'search'),
)

data = '[{"lat":3.003090,"lng":101.678300,"classification":"LOW_RISK_NS"}]'

response = requests.post('https://mysejahtera.malaysia.gov.my/register/api/nearby/hotspots', headers=headers, params=params, cookies=cookies, data=data)
response.json()

这是一个尝试。但是还是给出了400错误的请求

POST("https://mysejahtera.malaysia.gov.my/register/api/nearby/hotspots?type=search",
     body = list(lat = 3.003090, lng = 101.678300, classification = 'LOW_RISK_NS'), 
     add_headers(Host= 'mysejahtera.malaysia.gov.my',
                 Accept= 'application/json',
                 Connection= 'keep-alive',
                 `Content-Length`= '77',
                 Authorization= 'Basic N0ZFRkRCMTMtN0Q2MC00NEQxLUE5MTctM0',
                 `Content-Type`= 'application/json',
                 `Accept-Language`= 'en-MY;q=1, ms-MY;q=0.9',
                 `User-Agent`= 'MySejahtera/1.0.36 (iPhone; iOS 14.6; Scale/2.00)'), verbose())

最佳答案

这里的技巧似乎是将数据构造为命名列表的列表,以便按照 API 的预期将其编码为 json。

请注意,为了检索所需的响应,大多数提供的 header 被证明是不必要的(或者是由 httr 自动生成的)。

library(httr)

# data as list of named list
data <- list(
  list(
    lat = 3.003090,
    lng = 101.678300,
    classification = "LOW_RISK_NS"
  )
)

params <- list(type = "search")

response <- POST("https://mysejahtera.malaysia.gov.my/register/api/nearby/hotspots",
                 query = params,
                 body = data, 
                 add_headers(Authorization = "Basic N0ZFRkRCMTMtN0Q2MC00NEQxLUE5MTctM0"),
                 encode = "json"
)
content(response)
#> $hotSpots
#> list()
#> 
#> $zoneType
#> [1] "RED"
#> 
#> $messages
#> $messages$ms_MY
#> [1] "Hai {name}, terdapat 21 kes COVID-19 dalam lingkungan radius 1km dari lokasi ini yang dilaporkan dalam masa 14 hari yang lepas."
#> 
#> $messages$en_US
#> [1] "Hi {name}, there have been 21 reported case(s) of COVID-19 within a 1km radius from your searched location in the last 14 days."
#> 
#> 
#> $note
#> NULL

reprex package 创建于 2021-06-21 (v1.0.0)

一些解释

列表中的 JSON 与列表中的列表

在此处查看命名列表和命名列表的列表之间的 json 生成字符串中的差异(即添加方括号):

data <- list(lat = 3, lng = 101, class = "LOW")

# list
jsonlite::toJSON(data)
#> {"lat":[3],"lng":[101],"class":["LOW"]}

# list of a named list
jsonlite::toJSON(list(data))
#> [{"lat":[3],"lng":[101],"class":["LOW"]}]

reprex package 创建于 2021-06-23 (v1.0.0)

更简单的方法

在这里,我自己做了更多的编码工作,而不是让 httr 像上面的答案那样处理它。

请注意,与上面的第二个(列表的列表)json 字符串相比,值周围的方括号现在丢失了。这代表了 httr 所做的“拆箱”。

library(httr)

response <- POST("https://mysejahtera.malaysia.gov.my/register/api/nearby/hotspots?type=search",
                 body = '[{"lat":3.003090,"lng":101.678300,"classification":"LOW_RISK_NS"}]', 
                 add_headers(Authorization = "Basic N0ZFRkRCMTMtN0Q2MC00NEQxLUE5MTctM0",
                             "Content-Type" = "application/json")
)
content(response)
#> $hotSpots
#> list()
#> 
#> $zoneType
#> [1] "RED"
#> 
#> $messages
#> $messages$ms_MY
#> [1] "Hai {name}, terdapat 22 kes COVID-19 dalam lingkungan radius 1km dari lokasi ini yang dilaporkan dalam masa 14 hari yang lepas."
#> 
#> $messages$en_US
#> [1] "Hi {name}, there have been 22 reported case(s) of COVID-19 within a 1km radius from your searched location in the last 14 days."
#> 
#> 
#> $note
#> NULL

reprex package 创建于 2021-06-23 (v1.0.0)

关于python - 带标题的 R httr 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67968996/

相关文章:

python - 使用三次样条进行曲线拟合

r - 忽略R httr连接中的SSL错误

r - 修复要为x :y instead of only 1:y运行的函数

r - 我应该在 API GET 请求中的何处插入 API key ?

r - API 请求和curl::curl_fetch_memory(url, handle = handle) 中的错误:SSL 证书问题:证书已过期

python - 删除在 PROTECTED ForeignKey 下自动创建的模型

python - 如何使用 pandas 模块将带有标题的表格导入数据框

python - 在 CentOS 6.7 版(最终版)上导入 lxml 失败

r - tidyverse-将命名矢量转换为data.frame/tibble的首选方法

r - 与 ggplot 的 plotly 交互