首页 > 解决方案 > 管道工找不到请求正文

问题描述

我目前正在 Heroku 服务器上编写一个 webhook,由于某种原因,我很难获取存储在请求正文中的值,因为它似乎总是显示为NULL. 我的 app.R 脚本看起来像指南中的所有内容:

#app.R
library(plumber)
library(tidyverse)

port <- Sys.getenv('PORT')

pr <- plumb("plumber.R")

pr$run(
  host = '0.0.0.0',
  port = as.numeric(port)
)

我的管道工.R 文件是这样开始的。

library(plumber)
library(tidyverse)
#* Log some information about the incoming request
#* @filter logger
function(req){
  print('LOGGER')
  print( req)
  cat(as.character(Sys.time()), "-",
      req$REQUEST_METHOD, req$PATH_INFO, "-",
      req$HTTP_USER_AGENT, "@", req$REMOTE_ADDR, "\n")
  plumber::forward()
}

#* @filter bodyExists?
function(req, res){
  print( 'BODY FILTER')
  print( req$body)
  if (is.null(req$body)){
    res$status <- 404 
    return(list(error="Body not found"))
  } else {
    plumber::forward()
  }
}

每次收到请求时,控制台都会显示: 在此处输入图像描述

因为没有身体而停止。我尝试从多个来源发送请求,我知道正在发送请求的主体,但由于某种原因,管道工在到达我的 api 时找不到它。我一直在使用https://github.com/virtualstaticvoid/heroku-plumber-app作为模板,我看不出我在做什么与他们不同。

标签: rapiherokuwebhooksplumber

解决方案


应该使用req$postBody而不是req$body


推荐阅读