rexyai / RestRserve

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

Unicode characters in the path #125

Closed dselivanov closed 4 years ago

dselivanov commented 4 years ago

moved from #109

here's hello world built in example RestRserve.

And i request to endpoint this /hello/path/{name} using /hello/path/안녕.

image

but request using httr, It's fine.

GET("http://localhost:8080/hello/path/안녕") %>% 
    content()

here's hello world code example.


#!/usr/bin/env Rscript

## ---- load packages ----

library(RestRserve)

## ---- create handler for the HTTP requests ----

# simple response
hello_handler = function(request, response) {
  response$body = "Hello, World!"
}

# handle query parameter
heelo_query_handler = function(request, response) {
  # user name
  nm = request$parameters_query[["name"]]
  # default value
  if (is.null(nm)) {
    nm = "anonym"
  }
  response$body = sprintf("Hello, %s!", nm)
}

# handle path variable
hello_path_handler = function(request, response) {
  # user name
  nm = request$parameters_path[["name"]]
  response$body = sprintf("Hello, %s!", nm)
}

## ---- create application -----

app = Application$new(
  content_type = "text/plain"
)

## ---- register endpoints and corresponding R handlers ----

app$add_get(
  path = "/hello",
  FUN = hello_handler
)

app$add_get(
  path = "/hello/query",
  FUN = heelo_query_handler
)

app$add_get(
  path = "/hello/path/{name}",
  FUN = hello_path_handler,
  match = "regex"
)

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

Originally posted by @mrchypark in https://github.com/rexyai/RestRserve/issues/109#issuecomment-590407868

dselivanov commented 4 years ago

Answers:

1:

Please note that the URL you use is invalid - only ASCII character are allowed in URLs. httr does not process the URL nor does it encode it, so your invalid URL is passed as-is. If you want to pass non-ASCII characters in URLs, you have to encode them and handle the encoding on the receiving end as well - see RFC2396

2:

HTTP doesn't support anything other than ASCII in URLs - there is no provision to declare encoding in the URL. Only HTTP request body can specify encoding, so that's the only way to reliably transport non-ASCII content. Some browsers will re-code URLs typed by the user in UTF-8 byte sequences (using the %xx byte representation) in that case you have to make sure you interpret the payload in UTF-8 and not any other non-UTF-8 locale. In addition, if you want the output to be treated correctly, you have to also declare the response encoding accordingly - e.g. above you have to add

response$content_type="text/html; charset=UTF-8"

and depending on the locale you run R in you may have to make sure your objects are in UTF-8 encoding. Please refer to R documentation on character encoding and associated functions.

mrchypark commented 4 years ago

love this issue