rexyai / RestRserve

R web API framework for building high-performance microservices and app backends
https://restrserve.org
276 stars 32 forks source link

[BUG] POST request with content-type "application/x-www-form-urlencoded" failed #183

Closed jonekeat closed 2 years ago

jonekeat commented 2 years ago

Describe the bug

I have follow the example in RestRserve Getting Started, but I added an post method using add_post(), with content-type: application/x-www-form-urlencoded

The response I get is

> response
$url
[1] "http://localhost:8080/fib_post"

$status_code
[1] 415

$type
[1] "text/plain"

$headers
  [1] 48 54 54 50 2f 31 2e 31 20 34 31 35 20 43 6f 64 65 20 34 31 35 0d 0a 43 6f 6e 74 65 6e 74 2d 74 79 70 65 3a 20
 [38] 74 65 78 74 2f 70 6c 61 69 6e 0d 0a 53 65 72 76 65 72 3a 20 52 65 73 74 52 73 65 72 76 65 2f 31 2e 30 2e 30 3b
 [75] 20 52 73 65 72 76 65 2f 31 2e 38 2e 31 30 0d 0a 43 6f 6e 74 65 6e 74 2d 6c 65 6e 67 74 68 3a 20 35 38 0d 0a 0d
[112] 0a

$modified
[1] NA

$times
     redirect    namelookup       connect   pretransfer starttransfer         total 
     0.000000      0.000054      0.000056      0.000114      0.000000      0.002553 

$content
 [1] 75 6e 73 75 70 70 6f 72 74 65 64 20 6d 65 64 69 61 20 74 79 70 65 20 22 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78
[38] 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 22

The content always return

> rawToChar(response$content)
[1] "unsupported media type \"application/x-www-form-urlencoded\""

I can confirm the bug is at https://github.com/rexyai/RestRserve/blob/967c48c4c2d9bbb77c4c8c3bfe52711e23592497/R/ContentHandlersFactory.R#L87

To Reproduce

library(RestRserve)
app = Application$new()

fib_post_handler <- function(.req, .res) {
  .res$set_body("success")
  .res$set_content_type("text/plain")
}

app$add_post(path = "/fib_post", FUN = fib_post_handler)

backend = BackendRserve$new()
backend$start(app, http_port = 8080)

and in another R session:

library(curl)
body <- curl_escape(c("username", "password", "abc"))
body <- paste0(c("username", "password", "application"), "=", body, collapse = "&")
body <- charToRaw(body)
postfieldsize <- length(body)
url <- paste0("http://localhost:8080", "/fib_post")
hd <- new_handle()
handle_setopt(hd, useragent = "libcurl/7.64.1 r-curl/4.3", post = TRUE,
              postfieldsize = postfieldsize, postfields = body)
handle_setheaders(hd,
                  "Accept" = "application/json, text/xml, application/xml, */*",
                  "Content-Type" = "application/x-www-form-urlencoded")
response <- curl_fetch_memory(url, handle = hd)
rawToChar(response$content)

Expected behavior

I think it should output success text in the response$content call

Environment information

> sessioninfo::session_info()
- Session info  --------------------------------------------------------------------------------------------------
 hash: tear-off calendar, baby symbol, pound banknote

 setting  value
 version  R version 4.0.3 (2020-10-10)
 os       Windows 10 x64 (build 19043)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.1252
 ctype    English_United States.1252
 tz       Asia/Kuala_Lumpur
 date     2022-03-31
 rstudio  1.3.1093 Apricot Nasturtium (desktop)
 pandoc   NA

- Packages -------------------------------------------------------------------------------------------------------
 package     * version date (UTC) lib source
 backports     1.3.0   2021-10-27 [1] CRAN (R 4.0.5)
 bench         1.1.1   2020-01-13 [1] CRAN (R 4.0.3)
 bit           4.0.4   2020-08-04 [1] CRAN (R 4.0.3)
 bit64         4.0.5   2020-08-30 [1] CRAN (R 4.0.5)
 blob          1.2.2   2021-07-23 [1] CRAN (R 4.0.5)
 cachem        1.0.6   2021-08-19 [1] CRAN (R 4.0.5)
 callr         3.7.0   2021-04-20 [1] CRAN (R 4.0.5)
 checkmate     2.0.0   2020-02-06 [1] CRAN (R 4.0.3)
 cli           3.1.0   2021-10-27 [1] CRAN (R 4.0.5)
 crayon        1.5.0   2022-02-14 [1] CRAN (R 4.0.5)
 curl        * 4.3.2   2021-06-23 [1] CRAN (R 4.0.5)
 data.table  * 1.14.2  2021-09-27 [1] CRAN (R 4.0.5)
 DBI         * 1.1.1   2021-01-15 [1] CRAN (R 4.0.5)
 desc          1.4.0   2021-09-28 [1] CRAN (R 4.0.5)
 devtools      2.4.2   2021-06-07 [1] CRAN (R 4.0.5)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.0.5)
 fansi         0.5.0   2021-05-25 [1] CRAN (R 4.0.5)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.5)
 fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.3)
 glue          1.5.0   2021-11-07 [1] CRAN (R 4.0.3)
 hms           1.1.1   2021-09-26 [1] CRAN (R 4.0.5)
 jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.5)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.0.5)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
 memoise       2.0.0   2021-01-26 [1] CRAN (R 4.0.5)
 mime          0.12    2021-09-28 [1] CRAN (R 4.0.5)
 pillar        1.6.4   2021-10-18 [1] CRAN (R 4.0.5)
 pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.5)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.3)
 pkgload       1.2.3   2021-10-13 [1] CRAN (R 4.0.5)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.3)
 processx      3.5.2   2021-04-30 [1] CRAN (R 4.0.5)
 profmem       0.6.0   2020-12-13 [1] CRAN (R 4.0.3)
 ps            1.6.0   2021-02-28 [1] CRAN (R 4.0.5)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.3)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.0.5)
 Rcpp          1.0.8   2022-01-13 [1] CRAN (R 4.0.5)
 remotes       2.4.1   2021-09-29 [1] CRAN (R 4.0.5)
 RestRserve  * 1.0.0   2022-03-27 [1] CRAN (R 4.0.5)
 rlang         0.4.12  2021-10-18 [1] CRAN (R 4.0.5)
 rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.3)
 Rserve        1.8-10  2021-11-25 [1] CRAN (R 4.0.5)
 RSQLite     * 2.2.11  2022-03-23 [1] CRAN (R 4.0.5)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.0.3)
 sessioninfo   1.2.1   2021-11-02 [1] CRAN (R 4.0.5)
 stringi     * 1.7.5   2021-10-04 [1] CRAN (R 4.0.5)
 testthat      3.1.0   2021-10-04 [1] CRAN (R 4.0.5)
 tibble        3.1.5   2021-09-30 [1] CRAN (R 4.0.5)
 usethis       2.1.3   2021-10-27 [1] CRAN (R 4.0.5)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.0.5)
 uuid          1.0-4   2022-03-16 [1] CRAN (R 4.0.5)
 vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.0.5)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.0.5)

 [1] C:/Users/jonekeat/Documents/R/R-4.0.3/library

------------------------------------------------------------------------------------------------------------------
dselivanov commented 2 years ago

fixed in #184