XML Posts Without A UTF-8 Encoding May Lead To 400 Bad Request Errors

Posted November 28, 2011 at 9:27 AM by Ben Nadel

Tags: ColdFusion

Last week, Ryan Jeffords and I spent a solid hour debugging some API interactions between ColdFusion and Recurly. The API requests required the posting of XML content using CFHTTP and CFHTTPParam. The API calls worked 99% of the time; however, every now and then, one would come back with a "400 Bad Request" status code response. After banging our heads together, we finally figured out that the cause of the 400 Bad Request was the high ASCII values embedded in some people's names.

Outside of the Byte-Order-Mark (BOM) and leading white space, I've never had an issue parsing XML in ColdFusion. As such, it never occurred to me that character encoding would be cause for concern. And, to be frank, character encoding has always been a bit of a blind spot for me. The Recurly API, on the other hand, seems to require explicit encoding when XML content resides outside the standard 256 ASCII characters.

At first, we were passing in XML bodies like this:

  • <?xml version="1.0"?>
  • <data>
  • <message>
  • Here is a high ascii characters: [#chr( 9786 )#].
  • </message>
  • </data>

This XML body contains a high ASCII value but no character encoding. This would parse absolutely fine in ColdFusion (cause ColdFusion rocks!); but, this was giving us a 400 Bad Request response when posted to Recurly. Once we added the character encoding, the API requests went through perfectly:

  • <?xml version="1.0" encoding="utf-8"?>
  • <data>
  • <message>
  • Here is a high ascii characters: [#chr( 9786 )#].
  • </message>
  • </data>

Notice that we added:

encoding="utf-8"

... to the meta-data at the beginning of the XML document.

Slowly, I am beginning to wrap my head around character encoding. And, mostly, that just means explicitly specifying UTF-8 whenever possible, whether that's in an XML body, a CFContent tag, a HTML5 Meta tag, or a database column definition. In this case, we needed to include a UTF-8 encoding with our XML API requests in order to prevent XML parsing errors (400 Bad Request responses).




Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
May 22, 2013 at 11:07 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
Could your problem be that "users.id" is actually an ARRAY, not a single value? Perhaps try it again with "users.id[1]" (I only have CF8 here at work). ... read »
May 22, 2013 at 7:52 AM
Nested Views, Routing, And Deep Linking With AngularJS
Hi, Just a quick thank you. As it happens, for my own purposes, the pending ui-router work being done in native angular is likely the one I'll adopt, but your exploration, code and documentation of ... read »
May 22, 2013 at 4:43 AM
How Do You Use The ColdFusion CFParam Tag?
'<cfparam>' or 'isDefined()and <cfset>' performs the same task.Is there any difference? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools