4

我有一个函数,它接受一个字符向量作为参数,处理后返回一个结果。

我想使用管道工将其作为 API 公开。如何将 JSON 作为输入传递

我使用了以下代码

file_recommender = function(req,res,files){

  files = as.data.frame(files)
  files = files$name
  files = as.character(files)

  library(dplyr)

  return(files)
  }

在 http 请求中,我将数据发送为

http://127.0.0.1:8000/get_file_recommendation?files=[{"name":"pvdxmanager.h"}]

http://127.0.0.1:8000/get_file_recommendation?files={name":["pvsignmanager.cpp","pvdxmanager.cpp","pvorderoperationsmanager.h"]}
4

1 回答 1

0

我错过了可重复的示例,因此我不知道您到底想做什么以及您的具体问题是什么。您不在函数中使用reqand参数,而是在不使用它的情况下加载。如果您只想返回文件名,请查看下面的示例。resdplyr

plumber部分应该看起来像这样:

#* Return recommended files
#* @param files JSON containing filenames
#* @post /get_file_recommendation
file_recommender = function(files){
  files = fromJSON(files)$name
}

输入:

curl -X POST "http://127.0.0.1:3070/get_file_recommendation?files=%7B%22name%22%3A%22pvdxmanager.h%22%7D" -H  "accept: application/json"

对于以下 JSON:

{"name":"pvdxmanager.h"}

回复:

[
  "pvdxmanager.h"
]

输入:

curl -X POST "http://127.0.0.1:3070/get_file_recommendation?files=%7B%22name%22%3A%5B%22pvsignmanager.cpp%22%2C%22pvdxmanager.cpp%22%2C%22pvorderoperationsmanager.h%22%5D%7D" -H  "accept: application/json"

对于这个 JSON(你的无效):

{"name":["pvsignmanager.cpp","pvdxmanager.cpp","pvorderoperationsmanager.h"]}

回复:

[
  "pvsignmanager.cpp",
  "pvdxmanager.cpp",
  "pvorderoperationsmanager.h"
]
于 2020-07-21T12:53:34.257 回答