尝试通过 R vs Postman 访问 UMLS API 时出现 415 错误

时间:2021-02-28 02:14:07

标签: r postman httr

我可以通过 Postman 成功访问 UMLS 身份验证的端点,但是在将此代码移动到 R 并使用 httr 时,不断收到 415 错误。这似乎只在使用 x-www-form-urlencoded(而不是 json)时有效。

我的相关邮递员标头是: 内容类型:application/x-www-form-urlencoded 和 接受:/*/

并尝试在 R 中重新创建它:

library(httr)
auth_endpoint <- "https://utslogin.nlm.nih.gov/cas/v1/api-key"
auth_headers <- c("Content-Type" = "application/x-www-form-urlencoded",
                  "Accept" = "*/*")

getTGT <- function(endpoint, headers) {
  request_body <- list(apikey = "API_KEY_HERE")
  request <- POST(url = endpoint,
                  headers = add_headers(.headers = headers),
                  body = request_body
}

此请求返回 415 错误,我只能说这与 Content-Type 相关。我更习惯于使用 JSON,但这在 Postman 中也不起作用。我是否为 x-www-form-urlencoded 类型正确创建了请求正文?

1 个答案:

答案 0 :(得分:0)

终于弄清楚了 - 对于 x-www-form-urlencoded 内容,POST 调用中需要包含 encode = 'form'。

相关问题