You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用httr2请求欧盟农业食品API遇SSL_read错误,求解决方案

解决httr2请求欧盟农业食品局API时的SSL意外EOF错误

问题重现

使用httr2请求欧盟农业食品局牛肉价格API时遭遇SSL读取错误,代码如下:

library(tidyverse)
library(httr2)

req <- request("https://www.ec.europa.eu/agrifood/api/beef/prices?memberStateCodes=PT&years=2019,2020&months=1,3,9&weeks=5,6,7,8,40,41,42&beginDate=01/09/2019&endDate=02/02/2020&carcassCategories=heifers,cows")

resp <- req |> req_perform()

错误信息:

Error in req_perform():
! Failed to perform HTTP request.
Caused by error in curl::curl_fetch_memory():
! OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0

解决方案

  • 临时禁用SSL证书验证
    部分情况下服务器证书配置可能存在兼容性问题,可临时关闭证书验证(注意:仅用于测试,生产环境不推荐):

    library(tidyverse)
    library(httr2)
    
    req <- request("https://www.ec.europa.eu/agrifood/api/beef/prices?memberStateCodes=PT&years=2019,2020&months=1,3,9&weeks=5,6,7,8,40,41,42&beginDate=01/09/2019&endDate=02/02/2020&carcassCategories=heifers,cows") |>
      req_options(ssl_verifypeer = FALSE)
    
    resp <- req |> req_perform()
    
  • 更新依赖库
    错误可能源于curl或OpenSSL版本过旧,尝试更新R的curl包及系统级的OpenSSL库:

    install.packages("curl")
    

    系统层面更新方式:Windows可通过Chocolatey安装最新curl,macOS用Homebrew,Linux用对应包管理器(如apt、yum)。

  • 添加用户代理头
    部分服务器会拦截无标识的请求,模拟浏览器用户代理可避免被拒绝:

    req <- request("https://www.ec.europa.eu/agrifood/api/beef/prices?memberStateCodes=PT&years=2019,2020&months=1,3,9&weeks=5,6,7,8,40,41,42&beginDate=01/09/2019&endDate=02/02/2020&carcassCategories=heifers,cows") |>
      req_headers(`User-Agent` = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") |>
      req_options(ssl_verifypeer = FALSE) # 可结合证书禁用
    
    resp <- req |> req_perform()
    
  • 修正URL参数格式
    检查API参数是否符合要求,比如日期格式可能需要YYYY-MM-DD而非DD/MM/YYYY,改用req_url_query构建参数更规范:

    req <- request("https://www.ec.europa.eu/agrifood/api/beef/prices") |>
      req_url_query(
        memberStateCodes = "PT",
        years = c("2019", "2020"),
        months = c("1", "3", "9"),
        weeks = c("5", "6", "7", "8", "40", "41", "42"),
        beginDate = "2019-09-01",
        endDate = "2020-02-02",
        carcassCategories = c("heifers", "cows")
      ) |>
      req_options(ssl_verifypeer = FALSE)
    
    resp <- req |> req_perform()
    

内容的提问来源于stack exchange,提问作者NickD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:42:42