NUL when sending "=%"
I am working on API in plumber. While testing it with httr I noticed unexpected behaviour, I narrowed it down to this reprex:
This would be api.R
library(plumber)
#* @apiTitle Bug demo
#* Send dataframe as JSON
#* @post /write/
function(req){
cat("Function starts\n")
data <- jsonlite::fromJSON(req$postBody)
cat("Function done")
}
run that with plumber::plumb(file='api.R')$run(port = 8376)
then try to send this request:
library(dplyr)
problematic <-
data.frame(A = "=%") %>%
jsonlite::toJSON()
httr::POST("http://127.0.0.1:8376/write/",
httr::content_type_json(),
body = problematic,
encode = "raw",
httr::verbose())
What happens is as soon as request is received it causes error on API side:
rawToChar(out): embedded nul in string
I cannot handle this as error is returned before it even gets to filters (I have filters in my API, I skipped them for reprex).
What I also found:
- converting
data.frametolistremoves the error; on JSON level difference is wrapping everything in array vs not doing so - problematic request works fine with API if sent via e.g. postman
%=will work, each character individually as well- you can add additional signs between
=and%and it still causes error
Just to share that when trying to reproduce the issue with your plumber API and another tool than httr to test, I still get the same error. I tested it by sending [{"A":"=%"}] in a POST body using an API plug-in in my browser. (RESTclient on Firefox)
Not sure this is something with httr 🤔
httr has been superseded in favour of httr2, so is no longer under active development. If this problem is still important to you in httr2, I'd suggest filing an issue offer there 😄. Thanks for using httr!