Skip to content

Instantly share code, notes, and snippets.

@wch
Created February 26, 2019 17:41
Show Gist options
  • Save wch/7c10cb7ab89580fa85ae5bb97e12c074 to your computer and use it in GitHub Desktop.
Save wch/7c10cb7ab89580fa85ae5bb97e12c074 to your computer and use it in GitHub Desktop.
httpuv server that prints out headers
library(httpuv)
s <- startServer("0.0.0.0", 5000,
list(
call = function(req) {
txt <- as.list(req$HEADERS)
txt <- paste(capture.output(str(txt)), collapse = "\n")
txt <- paste0(req$REQUEST_METHOD, " ", req$PATH_INFO, " \n", txt)
cat(txt, "\n\n")
list(
status = 200L,
headers = list(
'Content-Type' = 'text/plain'
),
body = txt
)
}
)
)
# Output looks like this:
#
# GET /
# List of 9
# $ accept : chr "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
# $ accept-encoding : chr "gzip, deflate, br"
# $ accept-language : chr "en-US,en;q=0.9"
# $ cache-control : chr "max-age=0"
# $ connection : chr "keep-alive"
# $ cookie : chr "_gauges_unique=1"
# $ host : chr "localhost:5000"
# $ upgrade-insecure-requests: chr "1"
# $ user-agent : chr "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment