Curl转R代码后无法获取文件名列表,请求排查原因
问题排查:R代码调用API返回错误页面而非预期结果
我有一段可正常运行的Curl POST请求,调用https://historicdata.website.com/api/DownloadListOfFiles接口能正确获取文件名列表,但通过ChatGPT转换为使用httr和jsonlite包的R代码后,请求返回成功却得到错误页面。转换后的R代码存在多处参数不一致问题,现排查原因并给出修正方案。
原Curl代码
curl -X POST https://historicdata.website.com/api/DownloadListOfFiles -H 'content-type: application/json' -H 'ssoid: INPUT_SESSION_TOKEN_HERE' -d '{ "music":"Rock", "plan":"Basic Plan", "fromDay" : 1, "fromMonth": 3, "fromYear" : 2020, "toDay": 3, "toMonth" : 3, "toYear": 2020, "eventId": null, "eventName": null, "marketTypesCollection": [ "GUITAR" ], "countriesCollection" : [ "GB", "IE" ], "fileTypeCollection" : [ "M" ] }'
转换后的R代码
library(httr) library(jsonlite) session_token <- "INPUT_SESSION_TOKEN_HERE" # Define the URL and headers url <- "https://historicdata.website.com/api/DownloadListOfFiles" headers <- c( "content-type" = "application/json", "ssoid" = session_token) # Define the request body body <- list( music = "Rock", plan = "Basic Plan", fromDay = 1, fromMonth = 10, fromYear = 2023, toDay = 2, toMonth = 10, toYear = 2023, eventId = NULL, eventName = NULL, marketTypesCollection = "GUITAR", fileTypeCollection = "M" ) # Perform the POST request response <- httr::POST(url, httr::add_headers(.headers=headers), body = jsonlite::toJSON(body)) # Check response if (httr::http_error(response)) { stop("HTTP error: ", httr::http_status(response)$reason) } else { cat("Request successful.") } # Extract and print the content of the response content <- httr::content(response, as = "text") cat(content)
错误返回结果
Request successful. <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ngErrorRedirect</title> </head> <body> <div> Error </div> <script defer src="https://static.cloud.com/beacon.min.js/v8c1696crossorigin="anonymous"></script> </body> </html>
问题原因分析
- 日期参数不匹配:原Curl请求的日期范围是2020年3月1日至3月3日,转换后的R代码错误使用了2023年10月1日至10月2日,参数不符可能触发后端的日期验证逻辑,导致返回错误。
- 数组参数处理错误:原Curl中的
marketTypesCollection、countriesCollection、fileTypeCollection均为数组类型,但转换后的R代码将前两者中的两个设为单个字符串,还缺失了countriesCollection参数,后端无法正确解析参数结构,引发错误。 - JSON序列化配置问题:
jsonlite::toJSON()默认会将单元素数组自动简化为单个值,导致后端接收的参数类型与接口要求不符,需要设置auto_unbox = FALSE来保留数组结构。
修正后的R代码
library(httr) library(jsonlite) session_token <- "INPUT_SESSION_TOKEN_HERE" # 定义请求URL和头部 url <- "https://historicdata.website.com/api/DownloadListOfFiles" headers <- c( "content-type" = "application/json", "ssoid" = session_token ) # 定义请求体,严格匹配原Curl的参数 body <- list( music = "Rock", plan = "Basic Plan", fromDay = 1, fromMonth = 3, fromYear = 2020, toDay = 3, toMonth = 3, toYear = 2020, eventId = NULL, eventName = NULL, marketTypesCollection = list("GUITAR"), # 保留数组结构 countriesCollection = list("GB", "IE"), # 补全缺失的数组参数 fileTypeCollection = list("M") # 保留数组结构 ) # 发起POST请求,设置auto_unbox=FALSE确保数组不被简化 response <- httr::POST( url = url, httr::add_headers(.headers = headers), body = jsonlite::toJSON(body, auto_unbox = FALSE) ) # 检查响应状态 if (httr::http_error(response)) { stop("HTTP错误: ", httr::http_status(response)$reason) } else { cat("请求成功。\n") } # 提取并打印响应内容 content <- httr::content(response, as = "text") cat(content)
内容的提问来源于stack exchange,提问作者user2165379
相关产品推荐
相关产品推荐

