httr::content() error from plumber API: Error in x$headers[["Content-Type"]]

My question is mostly around what needs to be EXPLICITLY defined in the response from the plumber API. I put a serializer at the top, so I'm assuming that's enough for the API to fill the Content-type in the header of the response.

Here's the top and bottom of the API code. It's running and working.

#* @post /adhoc
#* @serializer text
function(req) {
 # ... my code ...
  list(
    status = 200L,
    request = req$body,
    body = list(status = api.status,
                adhoc = toJSON(adhoc.json,na = "null")))
}

Here is my httr call for communicating with the app

output <- POST("myapphost.com:23432/adhoc",
                body = list(user="me@company.com",
                            session_id="test",
                            q="Please give me data."),
                encode = "json")

But when I try and extract the content, here's my error:

> content(output)
Error in x$headers[["Content-Type"]] : subscript out of bounds

I've tried this so many ways... and I don't understand why httr's content() function can't extract the output and/or why plumber isn't attaching a content-type header by default

What do you get with rawToChar(output$content) ?

> rawToChar(content$output)
Error in content$output : object of type 'closure' is not subsettable

check again, content is a function of httr, i see content$output and i wrote output$content

sorry... that wasn't very smart. Looks like the only thing that made it out was the status.

> rawToChar(output$content)
[1] "200"

This definitely doesn't make any intuitive sense to me. The request body should be there in some shape or form, and the body should have something there... this doesn't seem to return like a function.

Intuitively, I'm thinking you are receiving a response that is not coming from your API, is it a proxy or a webserver. I do not know. Try playing around with plumber examples or simpler API to get a feel of how the pieces fit together.

Definitely not a proxy, and its being launched with a host=“0.0.0.0” argument as it’s a Linux vm on a govt network.

I’m planning on using pm2, but the directions say to launch it from Rscript or console first. I did a lot of print commands on the API’s side and verified everything is being processed correctly. I started with a json serializer and had the same error, then simplified to the text to try and get something back. I don’t understand how content would get stripped from a simple data response.

Why do you need pm2? If its a linux vm, it's pretty easy to just turn it into a service? That's beyond what I can help you with I think. You will eventually figure it out, I believe in the power of being unwilling to quit. :smiley:

I'm happier not needing it. The documentation lists systemd as the last option, and to a novice with plumber, that suggests to me that systemd should be treated as a last resort... one of my general frustrations with technical documentation is that I always have to read 20 blog articles to get a good feel for "these are general recommendations because [fill-in-the-blank with major and minor caveats]".

Anyway, I'm going to make some slim APIs in the plumber file and test list returns locally via another shell, and then via another linux vm. I still don't understand what kind of networking configuration would cause data to be stripped from the response. This seems like an anomaly as I can't even find stackoverflow or posit community questions that refer to a similar problem.

Note... I figured out the problem.

I was not including the "http://" protocol in the url. I guess I don't even understand enough about the internet to understand how in the world the POST was initiating the API to actually run along with data inside the request... but somehow it kicked off the process and passed my q field despite missing the "http://" in front of the url.

I'm extremely excited by your last comment, keep going. The more knowledge you acquire the easier it gets. Keep reasoning from first principle, explore all options, keep learning. You will come up with your own solutions, and, that, in itself, is very rewarding.