首页 > 解决方案 > 使用 r 中的 curl 为 DVLA MOT 数据制作 curl 命令

问题描述

尝试使用 curl 包在 r 中编写 curl 命令时,我感到非常迷茫。

我拥有的 curl 命令是

curl -H "Accept: application/json+v3" -H "x-api-key: XXXXX" \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?page=[0-58002]

我试过以下

library(curl)
h <- new_handle()
handle_setopt(h);
handle_setheaders(h,
                  'page'='[0-58002]',
                  'Accept' = 'application/json',
                  'x-api-key' = 'XXXXX'

)


tmp <- tempfile()
curl_download("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests", destfile = tmp, handle = h)

但这似乎不起作用。我究竟做错了什么?我知道最初的 curl 命令有效,而不是 r 版本。

标签: rapicurl

解决方案


我想你想要

library(curl)
h <- new_handle()
handle_setopt(h, verbose = TRUE)
handle_setheaders(h, .list = list(
              'Accept' = 'application/json',
              'x-api-key' = 'XXXXX')

)
tmp <- tempfile()
curl_download("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?page=[0-58002]", destfile = tmp, handle = h)

handle_setopt(h, verbose = TRUE)获得详细的 curl 输出,将帮助您调试任何问题。查询参数在 url 中,而不是在标题中


推荐阅读