rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

Calling API deployed to DigitalOcean generates 301 error #238

Closed glennlawyer closed 6 years ago

glennlawyer commented 6 years ago

Love the plumber package so far. Everything works on my local machine (running from R, running from a docker container), but when deploying to DigitalOcean, I cannot access the API using cURL. It is likely to be an IAK (idiot at keyboard) error, but the docs are a bit sparse and I don't see where I've gone wrong.

My question: What is the proper way to call the API using cURL?

I'm following this tutorial: http://www.exegetic.biz/blog/2017/06/deploying-minimal-plumber-api-digitalocean/, and deploying their toy API as follows:

id <- plumber::do_provision(unstable = TRUE, example = FALSE)
do_deploy_api(id, "date", "./api/", 8000)

where the file "api/plumber.R" is

#* @get /
function() {
  Sys.Date()
}

The instance builds and provisions on DigitalOcean as it should. Opening the URL http://159.XXX.XXX.XXX/date in my browser shows the date. That's where the tutorial ends, no cURL example.

But the following:

curl http://159.XXX.XXX.XXX/date

returns

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

Also tested it with my real api, which expects a post request. Again, the api works fine running locally on my machine or in a docker container. I try deploying to DigitalOcean via

do_deploy_api(drop, "moodgraph", "./deployAPI/", 8001)

which (according to the R console) deploys properly.

But the curl commands

curl http://159.XXX.XXX.XXX/moodgraph -d "@moods.json"

or

curl -X POST -d @moods.json http://159.XXX.XXX.XXX/moodgraph/ --header "Content-Type:application/json"

returns the same 301.

What is the proper way to call the API using cURL?

david-jankoski commented 6 years ago

i might be wrong but i think you need the -F parameter for posting the json file
curl -X POST -F @moods.json http://159.XXX.XXX.XXX/moodgraph/ --header "Content-Type:application/json"

glennlawyer commented 6 years ago

Solved it!!!

The problem was a path error. My plumber file defines the endpoint as

#* @post /moodgraph
function(moods)

My deploy file sets the endpoint as

do_deploy_api(drop, "moodgraph", "./digiocean", 8000)

Thus the proper cURL path is

http://159.XXX.XXX.XXX/moodgraph/moodgraph

where (I believe) the first "moodgraph" is the name set by do_deploy_api and the second "moodgraph" in the path is set by my plumber.R file

and

curl -X POST -d @moods.json http://159.XXX.XXX.XXX/moodgraph/moodgraph --header "Content-Type:application/json"

produces the expected result.

@david-jankoski Thanks for the tip.