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,
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/
Related
I need to configure JSON to JSON transformations with API Manager 2.0 very efficiently.
By default, WSO2 API Manager makes transformations JSON-> XML-> and after processing -> XML->JSON
I'd like to know how can I do so directly in JSON to reduce latencies.
Thank you for your help!!
By default, APIM uses PassThrough transport which does not build your message unless it needs to access your message body. So, your json won't be converted to XML in that case.
For example, if you have configured custom mediation and have a content-aware mediator like <log level="full"/>, APIM has to build (i.e. convert to XML) your message body so that it can log the entire message.
If you don't try to access the message body like that, json will be sent to backend as it is.
I have a problem in converting json to json in wso2 esb, actually I'm using payload factory in a proxy and I call the proxy with rest and json content. Here is my integration flow, I call a proxy and the proxy sends the request(with json content) to a jms message store then, I defined a message processor to consume messages from message store and send them to a defined endpoint(.net web api). but the problem is strings with numbers automatically get converted as integer elements: "orderId": 10000 ( I want it to be string "orderId": "10000") , but when I send the request directly from proxy to my end point (without using message broker) it works correctly. Could you please help me solve this problem?
For more details refer to my question with more details on this problem, and looks like this is a bug in wso2 esb 4.9.0
wso2 jira
For your problem need to change a JSON Message Formatters.
please follow the below steps for that.
change your working directory to [ESB Home]/repository/conf/axis2/axis2.xml
then you need to change JSON Message Formatters instead of JsonStreamFormatter use org.apache.axis2.json.JSONMessageFormatter.
<!--messageFormatter contentType="application/json" class="org.apache.synapse.commons.json.JsonStreamFormatter"/-->
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/>
You can use the following builder and formatter in axis2.xml
org.apache.synapse.commons.json.JsonStreamBuilder
org.apache.synapse.commons.json.JsonStreamFormatter
Remove existing builder and formatter for the "application/json" and add the below.
Add under Formaters section
<messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/>
Add under Builders section
<messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
I was given a URL that is supposed to return some JSON data which contains a oAuth Token to access the rest of the API. The API call uses GET and it contains the username and password in the URL.
When I try accessing the API's URL I get the following message:
error unauthorized
error_description An Authentication object was not found in the SecurityContext
Below is an example of my ColdFusion:
<CFHTTP URL="https://test-ows01.website.com/data_api//1.0/oauth/token?grant_type=password&username=HelloWorld&password=MyPassword!" method="GET" result="result">
</CFHTTP>
<cfset content = deserializeJSON(result.filecontent.toString())>
<cfdump var="#content#">
Other info: When I drop the URL (it is not the correct URL for this questions) into a browser I get the “Log In” prompt and after I enter the username: HelloWorld and the password: MyPassword! I am then taken to a page that shows the JSON data which contained the oAuth Token among other data.
I was given a SOAPUI file so review how the API works so if there is some other info I need to provide I may be able to pull it from there.
The server your connecting to is requiring authentication credentials during in the request. Add the username and password to the cfhttp tag to send the credentials within the request.
<cfhttp url="example.com/data_api//1.0/oauth/token?grant_type=password" method="GET" result="result"
username="HelloWorld" password="MyPassword!">
</cfhttp>
I have a http request I am trying to make on an afterSave method in my Cloud Code. I have been able to create my request, and when I console.log(response) it outputs a block that contains the information that I am after. I am aware that response.text is a string so I am trying to run JSON.parse(response.text) so I can access my API response.
I can print out what appears to be an object after running JSON.parse, but much of the data within my response is stripped out. I know it is not the fault of the API because I have another function that runs on the client with the same query and it works correctly.
What is the correct way to parse the response.text from a Parse.Cloud.httpRequest to maintain my data.
Try var result = JSON.parse(response['text']).
I'm struggling to find a way to send back JSON inside an object I'm returning (' running Coldfusion8' ) Currently I'm sending back this on Ajax request:
{
"SUCCESS":true,
"DATA": "JSON someData",
"COUNT":10
}
I also have content-only Ajax requests, where I'm just sending back the DATA part like so:
return "HTML someData"
This way I can send compressed and binary-gzipped HTML, which works nicely and reduces "payload" from 60k of data to 2-3k.
Since my page has to be available offline, I have started to do all markup enhancements on the client and only send JSON data from my database (I guess as it should be). However my JSON strings are far bigger than the inital HTML I was sending (say 10k JSON, that needs to be fiddled into elements I need to creat vs. 2-3k of ready to use snippets). So I'm punishing myself with extra bandwidth and client side processing.
To workaround, I started to try to gzip my JSON string (works) and whether I can send this back like in my 2nd example (doesn't work...):
return "JSON someData"
If I specifiy text/JSON in the return header as well as JSON in the ajax-returnformat, I'm getting only Coldfusion errors, telling me that Unable to serialize binary data to JSON
QUestion
Is it at all possible to send back JSON-gzipped? If so, what do I need to specifiy on my AJAX call (returnformat?) and server-side to make it work and not fail every time.
Thanks!
(NOTE: I would not ask, if I could set JSON GZIP encoding on the server!)
Ok. You can "beat the returntype" by using:
<cfif variables.alredayBinary EQ "false">
<cfcontent reset="no" variable="#CharsetDecode(passBackObject, "UTF-8")#" />
<cfelse>
<cfcontent reset="no" variable="#passBackObject#" />
</cfif>
<cfreturn />
Returning nothing, because as I understand <cfcontent> goes straight back to the client. Maybe useful for someone else, too.