4

I'm writing and API in R using plumber that ideally will consume the JSON it receives on POST. But I cannot get the endpoint POST example to work that way, so I'm probably missing something obvious.

Using the example URL and Curl I can do the following without issue:

curl -i -X POST http://plumber.tres.tl/append/append -d "val=50" 

But the way the example is presented:

POST {val: 50} -> http://plumber.tres.tl/append/append

Suggests that JSON would also be allowed. So I have tried:

 curl -H "Content-Type: application/json" -X POST -d '{"val":50}' http://plumber.tres.tl/append/append

And all the variation to ensure UTF-8 encoding, comment out the " and all kinds of other combinations based mostly on what I found here on Stackoverflow about post. For example:

curl -i -X POST -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{"val":50}'
curl -i -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{\"val\":50}'
curl -i -X POST -H "Content-Type: application/json;charset=UTF-8" http://plumber.tres.tl/append/append -d '{"val":50}'

Also using a file and trying to post it as @my.json did not work.

Maybe it is something on the Plumber side: I would expect that given the toolset to serialize the output, I can also state the expected serialization of the input. But I have not found how to do that.

FvD
  • 3,697
  • 1
  • 35
  • 53

1 Answers1

5

This turned out to be a relatively simple issue with plumber. The function postBodyFilter calls parseQS that in turn splits on & and = and does not yet check for JSON formats (for example based on an initial { and ending }).

Since jsonlite was already imported by the package I proposed a small change to add basic JSON support in pull request #53.

Following the example in the README, the following will work after adding this patch:

 curl --data '{"a":4, "b":5}' http://localhost:8000/sum

Since the call is on jsonlite to parse the content of the querystring, more complex JSON should also be possible, but I have not tested that yet.

Update : This has now been merged into the plumber project and will work for you if you install the version from github using devtools::install_github("trestletech/plumber"), or through a traditional install as soon as version 0.3.1 is available on CRAN.

FvD
  • 3,697
  • 1
  • 35
  • 53
  • I have an R code pipeline that sends GET call to qualtrics to pull the survey and process it. Instead of setting up this R pipeline as a cron job, I would like the code to be triggered by user submit button on qualtrics. Do you know how to achieve it ? Any direction or material to begin with will help. Thankyou – user5249203 Jul 08 '21 at 18:10
  • If I understand correctly what you are trying to do, you can set up an endpoint as a POST in plumber so that you can trigger that function that whenever the user presses the submit button. Have a look at the "routing and endpoints" documentation in plumber: https://www.rplumber.io/articles/routing-and-input.html?q=post#endpoints – FvD Jul 09 '21 at 17:03