2

I'm attempting to pull the variable GetHttpRequestData().headers.accept-language in ColdFusion 2018, but am getting an error.

  1. The main object GetHttpRequestData().headers looks fine. This is information from the html request header.

  2. I can also pull back other values in the headers object like GetHttpRequestData().headers.host or GetHttpRequestData().headers.accept

  3. However, if the variable name contains a dash ie. GetHttpRequestData().headers.accept-language, I get an error because CF Thinks I'm executing a mathematical function.

How can I return this variable? It should be simple.

cffiddle example

// Dump the Object    
writeDump(var="#GetHttpRequestData().headers#",format="html")

// Dump a result in the Object OK
writeDump(var="#GetHttpRequestData().headers.host#",format="text")

// Dump a result in the Object OK
writeDump(var="#GetHttpRequestData().headers.accept#",format="text")

// CF Thinks I'm executing a mathematical function
// when structure variable has a dash in the name
try {
    writeDump(var="#GetHttpRequestData().headers.accept-language#",format="text")
} catch (any e) {
    writeOutput("Error: " & e.message);
} 
Community
  • 1
  • 1

1 Answers1

3

Answer to ColdFusion variables with dash separators.

Use the chained variable in the following manner:

 #GetHttpRequestData().headers['accept-language']#
  • note the drop of the point separator
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 1
    Yep. FYI, when using dot-notation you're limited to keys that conform to CF's [variable naming rules](https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/using-coldfusion-variables/creating-variables.html) (i.e. must start with a letter/underscore, can't contain spaces or other invalid characters like a dash, etc...). Whenever dealing with exotic or invalid variable names, use structure (or associative array) notation instead. – SOS Apr 16 '19 at 13:02