0

I would like to get the raw text of a POST request's body using the R web server package Rook.

I have registered the R app:

parsePOST <- function(env) {
  request <- Rook::Request$new(env)

  body <- request$body()
  print(body)
}

R.server$add(app = parsePost, name = "reportGeneratorApp")

Unfortunately, body is a Rook::RhttpdInputStream. Things I've tried:

  1. This provides only the first line:
    body <- request$body()$read_lines()
    Result:
    {\n

  2. This provides only the start of the body, regardless of the "Content-Length" header. All other read lines are character(0). Additionally, the lines have missing data in random places.
    body <- request$body()$read_lines(10)
    Results:

   [2] " \"data\": {\n   "                                                                                                                        
   [3] "evious\": [\n      2\n    ],\n    \"v"                                                                                                    
   [4] "e\": {\n        \"Differentially private me"                                                                                              
   [5] "              \"values\": [\n                44.39"                                                                                       
   [6] "an mechanismLaplace\"\n              ],\n              \"arguments\": ["                                                                  
   [7] "         },\n            \"batch\": [\n              1\n            ]\n          }\n    "                                                 
   [8] "\n            ]\n          }\n        }\n      },\n      \"educ_income\": {\n        \"ols\": {\n          \"ols0\": {\n            \"rel"
   [9] ""                                                                                                                                         
  [10] ""                                                                                                                                         

The POST request is being sent by postman, with the header Content-Type: application/json.

I'm very surprised to find it so difficult to retrieve the request body.

Shoeboxam
  • 43
  • 1
  • 5

1 Answers1

0

I was able to extract the body as a string with the following:

writeLines(paste0(rawToChar(request$body()$read(), multiple = T), collapse=""))

The read() method returns bytes, I parse each byte into a character vector, and then collapse the character vector. If anyone has an easier or cleaner way to do this, please share.

Shoeboxam
  • 43
  • 1
  • 5