4

I have a function that takes in a character vector as argument and after processing returns a result.

I want to expose it as an API using plumber. How to I pass a JSON as input

I have used the following code

file_recommender = function(req,res,files){

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

  library(dplyr)

  return(files)
  }

In the http request I am sending the data as

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"]}
NinjaR
  • 621
  • 6
  • 22

1 Answers1

0

I miss the reproducible example, therefore I don't know what exactly you want to do and what's your concrete problem. You don't use the req and res argument in your function and you load dplyr without using it. If you just want to return the file names, look at the example below.

The plumber part should probably look like this:

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

Input:

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"

for the following JSON:

{"name":"pvdxmanager.h"}

Response:

[
  "pvdxmanager.h"
]

Input:

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"

for this JSON (yours is not valid):

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

Response:

[
  "pvsignmanager.cpp",
  "pvdxmanager.cpp",
  "pvorderoperationsmanager.h"
]
nachti
  • 1,086
  • 7
  • 20