I am trying to loop over a cookie coming from cfhttp but it is not displaying correct results.
Below is my code
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false">
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
<cfhttpparam type="cookie" name="#cookieName#" value="#cookievalue#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
Second attempt:
From one call, I am getting cookies and I am putting them in a structure as follows:
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset cookieStruct=StructNew()>
<cfloop collection="#cookies#" item="key">
<cfset cookieKeyAndValue = REReplace(key, ";.*", "")>
<cfset cookieKey = listfirst(cookieKeyAndValue,'=')>
<cfset cookieValue = listlast(cookieKeyAndValue,'=')>
<cfset StructInsert(cookieStruct,cookieKey,cookieValue)>
</cfloop>
<cfdump var="#cookieStruct#" abort>
<cfhttp url="#addr#" method="get" throwOnError="Yes" resolveurl="false" result="objAddress">
<cfloop collection="#cookieStruct#" item="key">
<cfhttpparam type="cookie" name="#key#" value="#cookieStruct[key]#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
This is giving me an error:
Invalid collection ASPJGASGHSG=KBHFPN; path=/. Must be a valid structure or COM object. Loop error.
CFHTTP GET returns a result into the CFHTTP data structure. Having the loop inside the CFHTTP open/close tag results in trying to loop over something that does not exist yet.
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false" >
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
</cfloop>
I'm not sure what you are attempting to do here but if there is more than one cookie you're going to need different code in the loop.
Related
We have a REST API that returns a stream of content-type application/pdf. I just want to save it to a file on the server.
<cfhttp url="#ApiPath#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cffile action = "write" file = "#FileName#" output = "#res.fileContent#">
I've producing a blank PDF, any ideas? ( ive tried various combinations of cfdocument/cfpdf with no luck)
here's a dump of the REST response:
I think I've got it:
Solution 1:
<cfhttp url="#url#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset bytes = res.FileContent.toByteArray()>
<cfscript>
fos = createObject("java", "java.io.FileOutputStream").init("myfile.pdf");
fos.write(bytes);
fos.flush();
fos.close();
</cfscript>
EDIT: Based on SOS's solution this also worked:
Solution 2:
<cfhttp url="#url#" method="post" result="res" getAsBinary="Auto">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset fileName = listlast(res["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="#path#\#fileName#" output="#res.FileContent#">
I am trying to take an existing ColdFusion website that currently uses Authorize to process cards. I would like to switch over the payment gateway and use Square. Does anyone have any sample ColdFusion code on how to use the Square API as a payment gateway?
Here is a gist another developer has used
<cfset IDKey = CreateUUID()>
<cfset request.params.card_nonce = form.nonce>
<cfset request.params.amount_money.amount = 100>
<cfset request.params.amount_money.currency = 'USD'>
<cfset request.params.idempotency_key = IDKey>
<cfset jsonString = serializejson(request.params)>
<cfset requestPath = "https://connect.squareup.com/v2/locations/<replace_locationid>/transactions">
<cfhttp url="#requestPath#" method="post" result="response">
<cfhttpparam type="HEADER" name="Accept" value="application/json">
<cfhttpparam type="HEADER" name="Content-Type" value="application/json">
<cfhttpparam type="HEADER" name="Authorization" value="Bearer <replace_access_token>">
<cfhttpparam type="body" name="params" value="#jsonString#">
</cfhttp>
I have the following Updated code where I am trying to connect to the website using the username/password and siteid combination, i am passing all values using get method and trying to fetch cookie to authenticate and go ahead but somehow i am again getting redirected to the login screen
My Code
<cfhttp method="get" url="https://stats.ezhostingserver.com/Login.aspx" resolveurl="true" redirect="false">
<cfhttpparam type="URL" name="ctl00$MPH$txtUserName" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtPassword" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtSiteId" value="*****">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset stm_cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset stm_temp = REReplace(stm_cookies, ";.*", "")>
<cfset stm_cookieName = listfirst(stm_temp,'=')>
<cfset stm_cookievalue = listlast(stm_temp,'=')>
<cfhttp method="get" url="https://stats.ezhostingserver.com/default.aspx" charset="utf-8" result="results" redirect="no">
<cfhttpparam type="cookie" name="#stm_cookieName#" value="#stm_cookievalue#">
</cfhttp>
<cfoutput>#results.filecontent#</cfoutput>
But it is saying me object moved error
My authorisation header looks like this (params altered slightly for security and line breaked for easier reading):
<cfset oAuthHeader = 'OAuth oauth_consumer_key="zz3u0Lf9XxkC2KX839r2MS0fDltvLquow3ZMLaOw",
oauth_nonce="9BD4FAE88D1B213F86D908FE183F0501C682EE2F",
oauth_signature="Zy91IhXWGcMxyuAVIlGX%2F3ULTWU%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1337169270",
oauth_version="1.0"'
My cfhttp call looks like this:
<cfhttp url="#oRequestReq.getNormalizedHttpURL()#" method="POST">
<cfhttpparam type="header" name="Authorization" value="#oAuthHeader#">
<cfloop collection="#arguments#" item="key">
<cfif key neq 'fieldnames'>
<cfhttpparam type="formfield" name="#key#" value="#arguments[key]#">
</cfif>
</cfloop>
</cfhttp>
Running <cfdump var="#GetHttpRequestData()#">, I get the following, which shows that my fields are passing through as formfield params OK, but my Authorization header is nowhere to be seen.
![enter image description here][1]
Shouldn't the Authorization header be included in the Headers struct?
[1]: http://i.stack.imgur.com/VbQQO.jpg
How are you getting the oauth_signature? It's not a hard-coded thing in OAuth - it's being generated each time.
I'd suggest using this library http://oauth.riaforge.org/
There are some examples there that should help you get started.
Shouldn't it be...
<cfset oAuthHeader = {
'oauth_consumer_key'="zz3u0Lf9XxkC2KX839r2MS0fDltvLquow3ZMLaOw",
'oauth_nonce'="9BD4FAE88D1B213F86D908FE183F0501C682EE2F",
'oauth_signature'="Zy91IhXWGcMxyuAVIlGX%2F3ULTWU%3D",
'oauth_signature_method'="HMAC-SHA1",
'oauth_timestamp'="1337169270",
'oauth_version'="1.0"
}>
<cfhttp url="#oRequestReq.getNormalizedHttpURL()#" method="POST">
<cfloop collection="#oAuthHeader#" item="key">
<cfhttpparam type="header" name="#key#" value="#oAuthHeader[key]#">
</cfloop>
<cfloop collection="#arguments#" item="key">
<cfif key neq 'fieldnames'>
<cfhttpparam type="formfield" name="#key#" value="#arguments[key]#">
</cfif>
</cfloop>
...
</cfloop>
?
I am trying to post to twitter. I have the app already authenticated and now want to post an update.
This is what my http post is at:
<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="post">
<cfhttpparam type="header" name="status" value="#urlEncodedFormat('my test post')#" />
<cfhttpparam type="header" name="oauth_consumer_key" value="xxx" />
<cfhttpparam type="header" name="oauth_nonce" value="xxx" />
<cfhttpparam type="header" name="oauth_signature_method" value="#urlEncodedFormat('HMAC-SHA1')#" />
<cfhttpparam type="header" name="oauth_token" value="xxx" />
<cfhttpparam type="header" name="oauth_timestamp" value="#GetTickCount()#" />
<cfhttpparam type="header" name="oauth_version" value="1.0" />
</cfhttp>
Has anyone done this? Am I going down the right route?
have you read this?
http://dev.twitter.com/pages/auth#auth-request
you need to construct the "signature base string" and post as body (warning: untested code, for CF8+)
<cffunction name="makeSignatureBaseString" returntype="string" output="false">
<cfargument name="httpMethod" type="string" required="true">
<cfargument name="baseUri" type="string" required="true">
<cfargument name="values" type="struct" required="true">
<cfset var signatureBaseString = "#httpMethod#&#URLEncodedFormat(baseUri)#&">
<cfset var keys = StructKeyArray(values)>
<cfset var key = "">
<cfset ArraySort(keys, "textNoCase")>
<cfloop array="#keys#" index="key">
<cfset signatureBaseString &= URLEncodedFormat("&#key#=#values[key]#")>
</cfloop>
<cfreturn signatureBaseString>
</cffunction>
-
<!--- using values from http://dev.twitter.com/pages/auth#auth-request --->
<cfset params = {
oauth_consumer_key = "GDdmIQH6jhtmLUypg82gる",
oauth_nonce = "oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y",
oauth_signature_method = "HMAC-SHA1",
oauth_token = "819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw",
oauth_timestamp = "1272325550",
oauth_version = "1.0"
}>
<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="POST">
<cfloop collection="#params#" item="key">
<cfheader type="header" name="#key#" value="#params[key]#">
</cfloop>
<!--- add status to the params for makeSignatureBaseString() --->
<cfset params.status = "setting up my twitter 私のさえずりを設定する">
<cfhttpparam type="body"
value="#makeSignatureBaseString('POST', 'http://api.twitter.com/1/statuses/update.json', params)#">
</cfhttp>