Coldfusion, JSON, and the Amazon CloudSearch API
Hi all,
We are using Amazon CloudSearch service for our site search. We created a front end which uses jquery to communicate with the server to display search results.
The problem we are running into is with creating and updating data on the backend. We have been creating new domains and uploading whole data sets manually from spreadsheets which is obviously not going to work for us long term.
The cloud search uses has an API that we can send JSON to for updating the items. I have created the nested json like this (the query name and data has been changed to protect the innocent):
<cfscript>
variables.dataFields = {};
variables.dataFields['id'] = " #getsdata.id#";
variables.dataFields['lang'] = "en";
variables.dataFields['type'] = "add";
variables.dataFields['version'] = " #DateFormat(Now(),"YYMMDD")##TimeFormat(Now(),"HHMM")#";
variables.dataFields['fields'] = {};
variables.dataFields.fields['redfield'] = "#getsdata.redfield#";
variables.dataFields.fields['blackfield'] = "#getdata.blackfield#";
variables.dataFields.fields['purplefield'] = "#getdata.purplefield#";
| variables.dataFields = serializejson(variables.dataFields); |
</cfscript>
When I dump that json out, it looks pretty good (there are many more fields). For now, I added a space before numbers to work around a bug I read about in the serializedjson function in CF8 that sees all numbers as numbers even if they are expressed as strings. I will figure that out later.
The code to post the json is:
<cfhttp url="http://doc-ourcompanydomain-abunchofnumbers.us-west-2.cloudsearch.amazonaws.com/2011-02-01/documents/batch" method="post" result="httpResp">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="header" name="Accept" value="application/json" />
<cfhttpparam type="header" name="Content-Length" value="#Len(variables.dataFields)#" />
<cfhttpparam type="body" value="#variables.dataFields#" />
</cfhttp>
The response I get is "400 Bad Request"
I know that there aren't that many people using cloud search and Coldfusion but I was hoping somebody might see a noob mistake in my code or methods.
Thanks!
Red
