Dogan Askan
Dogan Askan

Reputation: 1238

How to get client IP address in R Plumber

I've checked GitHub Repo and doc but still couldn't figure out how to get client IP in Plumber.

Here is the implementation I tried, I want to add IP addresses for all requests into the log file,

#' @post /v1/rl
rl_v1 <- function(a, b, c){
  request='rl'
  start_time <- as.numeric(as.POSIXct(Sys.time()))
  log_record <- paste(NULL, Sys.time(), request, "requested", NULL, NULL, 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  lhs <- data.frame(a=unlist(a),
                b=unlist(b),
                c=unlist(c))

  pairs <- custom_function(lhs, rhs, m_w = 0.98,
                                ext_blk_field=c(12), international=T,
                                fasterWcoBlock=T, preprocessedData2=T)
  input_records=nrow(lhs)
  matches=nrow(pairs)
  query_time <- as.numeric(as.POSIXct(Sys.time())) - start_time
  status <- data.frame(query_time=query_time, 
                   request=request, 
                   type='POST',
                   api_version=api_version_v1)

  log_record <- paste(NULL, Sys.time(), request, "responded", 
                  round(matches/input_records*100, 2), 
                  paste0(matches, '/', input_records, ' in ', query_time), 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  return(list(data=pairs, status=status))
}

Any help is highly appreciated.

Upvotes: 2

Views: 865

Answers (1)

r2evans
r2evans

Reputation: 160492

To close out the question, I'll restate the comment:

Since plumber uses httpuv, it's possible you can reach the req$REMOTE_ADDR property of the request handle.

Upvotes: 6

Related Questions