rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

How to access header where there "-" in names #155

Closed kush99993s closed 7 years ago

kush99993s commented 7 years ago

I am trying to pass header "X-Name" from postman then access it from API which is build using Plumber R.

However, I am not able to access it. using following code,

req$HTTP_X-NAME, then i am getting following error.

Error in (function (id, time_frame, req, res) : object 'name' not found\n"

If I use following code, I get NULL values,

req$HTTP_X_NAME  

or

req[["HTTP_X-NAME"]

or

req[["HTTP_X-Name"]

Following is my code:

#* @get /
healthCheck <- function(){
  print("It's Running")
}

#* @param id:int Random Number
#* @post /test/values
#* @get /test/values
test <- function(id, req, res){
  print(req[["HTTP_X-Name"]])
  return(req[["HTTP_X-Name"]])
}
trestletech commented 7 years ago

Hi. This is working for me. The proper name is all-caps with hyphens substituted for underscores like you have in your second example.

#* @get /
healthCheck <- function(){
  print("It's Running")
}

#* @param id:int Random Number
#* @post /test/values
#* @get /test/values
test <- function(id, req, res){
  print(ls(req))
  print(req[["HTTP_X_NAME"]])
  return(req[["HTTP_X_NAME"]])
}

Then I can run

$ curl --header "X-Name: hithere" http://localhost:9824/test/values
["hithere"]
kush99993s commented 7 years ago

Thanks, it is working for me 👍