Tuesday, December 31, 2002

Bug in CFHTTP's FirstRowAsHeaders when COLUMNS not used

There appears to be a bug in the function of the new FirstRowAsHeaders attribute for CFHTTP. It's not working when the COLUMNS attribute is not specified, which is counter to the indication of how it should work in the docs.

CFHTTP adds a new firstrowasheaders attribute for working with reading in of text files. This is useful in case the firstrow of a text file (such as a CSV or comma-delimited file) doesn't have a list of column names. By adding the NAME attribute, CFHTTP will read the text file and turn it into a CF query result set. (The ability to read in text files and turn them into queries isn't new, of course: just the ability to indicate FirstRowAsHeaders.)

But the docs (CFML Reference) say that if it's set to "no" then 'If the columns attribute is not specified: processes as data, and generates column names; for example, "column_1"'

That would suggest that CF would create some column names automatically, which would be cool when one has a very large number of columns in the file being read. But I just tried it and simply got the error "The column name "1" is invalid." because that was the data in the first column of data in the first row.

It's as if it's ignoring the firstrowasheaders="no" attribute and trying to read the first row data as the column names. That attribute is indeed working if I add a COLUMNS attribute and give it column names, but again the point is that one shouldn't have to according to the docs.

I just reported this on the cfbughunt.org site and added a comment about it to the livedocs.macromedia.com site for that page in the reference manual.

BTW: I've found a work-around. If you know the number of columns in the file being read, you can just run a little loop to create the column names automatically, as in:

<cfset colnames="">
<cfloop from="1" to="7" index="i">
<cfset colnames = listappend(colnames,"column_"&i)>

Then use that created "colnames" value in the CFHTTP as COLUMNS="#colnames#". Unfortunately, you have to know exactly how many columns to use or a failure to specify the right amount will get an error when CF tries to read the file and matches the number it finds against the number of columns you generated. It would be nice if it just did itself as it seems it's supposed to.

No comments: