11

This is the first time I'm writing a cfc that will catch JSON data from an external web server that would be posting information.

I'm working with a new service that can be set to send us, via HTTP POST to a URL I specify, a JSON packet of information regarding failed transactions.

I figured I'd setup a CFC with remote access to capture and deserialize the JSON data into something we could then act on. However, I can't figure out how to setup the function in the CFC to received the data?

I set the URL to www.mydomain.com/com/processRemote.cfc?method=catchJSONdata&ReturnFormat=json

To test it, I setup a simple test page that should post session data:

<cfhttp 
  result="result"
  method="post" 
  url="http://www.mydomain.com/com/processRemote.cfc?method=catchJSONdata&ReturnFormat=json">

    <cfhttpparam type="header" name="content-type" value="application/json"/>
    <cfhttpparam type="body" value="#serializeJSON(session)#"/>

So where I'm lost is what's the cfargument name that I'd have in my cfc that I would initially store the JSON data in? I have no control over the remote service that would be sending the JSON data.

Thanks,

Steve
  • 2,052
  • 2
  • 19
  • 37

1 Answers1

14

If you're reading content from the HTTP request body you wont find it in arguments scope - you'll need to extract it directly from the request:

if (cgi.content_type EQ "application/json")
{
    myData = deserializeJSON(ToString(getHTTPRequestData().content));
}

I use the Taffy[1] framework for building services like this (Disclaimer: I actually helped write the part of the framework that handles this case).

[1] http://atuttle.github.com/Taffy/

bpanulla
  • 2,862
  • 1
  • 17
  • 23
  • Thanks bpanulla! I was trying to create a cfc that would accept the posted data directly. Using your answer I created a cfm page that accepts the data, makes sure it's what we're expecting, and then hands it off to a cfc for processing. – Steve Apr 25 '11 at 20:47
  • That same technique should work just as well within your CFC method. Your CFC doesn't need to define any arguments (CFARGUMENT or script). – bpanulla Apr 27 '11 at 00:39
  • This had been bugging the hell out of me for hours... If I'm receiving a post, I'd expect the data in the Form scope! Apparently not if it's JSON posted in the body. Ahh well, lesson learned. Cheers. – Gary Stanton Nov 08 '12 at 17:22