[Developers] coldfusion

Mark CE markce at btclick.com
Mon Aug 6 17:46:00 UTC 2007


Don't worry I have tried both POST and GET, many times now!

Two crappy CF templates attached (testme.cfm which calls 
testme2.cfm).  I am not familiar with this list; if it doesn't accept 
attachments and I should be including the code in the body of this 
email, I guess I'll find out when my message comes back.

Needless to say, the cookie-getting function does work and 
successfully returns the two cookies:
metaweb-user and metaweb-user-info.

-----------------------
Originally I wasn't deserialising the JSON response so hence the 
unhelpful error message.
Now the response is:

code: /api/status/error/input/invalid
message: authentication required
----------------------

Thanks again
Mark


At 16:34 06/08/2007, you wrote:

>In addition to the cookie requirement, you probably need to do your
>cfhttp call as a POST, cfhttp is a GET by default.  If that doesn't
>work post your cfhttp code here to the list.
>
>On 8/6/07, Mark CE <markce at btclick.com> wrote:
> >
> >
> >  Hello there,
> >  Has anyone successfully retrieved a query result from Freebase using
> > Coldfusion CFHTTP?
> >  Or know of a reason why mine would return a 400 error?
> >
> >  HTTP/1.0 400 Bad Request X-Metaweb-Cost: dt=0.001, nreqs=0 Date: Fri, 03
> > Aug 2007 16:10:49 GMT X-Metaweb-TID:
> > cache;cache01.p01.sfo1:8101;2007-08-03T16:10:49Z;0002
> > Server: Apache/2.0.58 Vary: Accept-Encoding Cache-Control: no-cache
> > Content-Type: applic
> >
> >  If instead of CFHTTP I get my coldfusion code to output the URL to the
> > page; I copy and paste into the address bar, I get a 200 
> response... and the
> > query results I would expect.
> >
> >  Working code samples gratefully received!!!
> >  Thanks
> >  Mark CE
> >
> >
> >
> >  ________________________________
> >  Mark Christian-Edwards
> >  markce at btclick.com
> >
> >
> >
> >
> > _______________________________________________
> > Developers mailing list
> > Developers at freebase.com
> > http://lists.freebase.com/mailman/listinfo/developers
> >
> >
>
>
>
>--
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.5.476 / Virus Database: 269.11.6/938 - Release Date: 
>05/08/2007 16:16
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20070806/798e717c/attachment.htm 
-------------- next part --------------
 <html>
<head><title>Metaweb Query Editor</title>
<style> /* CSS styles for nice output */
#q, #r { width: 400px; height: 300px; border-width:0px;padding:5px;}
th {background-color:black; color:white; font:bold 12pt sans-serif;}
td.border,th {border:solid black 3px;}
input { margin: 5px; font-weight: bold; }
table { border-collapse: collapse;}
</style>
</head>
<body>

 <!-- Form makes an HTTP GET request to mqlread, results go in iframe r -->
  <form action="testme.cfm"
        method="post">
  <!-- Force mqlread to return text/javascript instead of application/json -->
  <input type="hidden" name="callback" value=" ">
  <!-- An HTML table to display the query, buttons, and result -->

  <table> 
    <tr><th>Query</th><td></td><th>Result</th></tr>    <!-- table header -->
    <tr>

      <td class="border">
        <!-- enter query -->
        <textarea name="queries" id="q">
{
  "qname": {
    "query": [{
      
  "type" : "/music/artist",
  "name" : "The Police",
  "album" : []

    }]
  }
}
 
        </textarea></td> 
      <td valign="top"> 
        <input type="submit" value="Send"><br>         <!-- send query -->

        <input type="reset" value="Erase"></td>        <!-- erase query  -->
      <td class="border">
        <iframe name="r" id="r"></iframe></td>         <!-- results go here -->
    </tr>

  </table>
  </form>
  
 <cfif isdefined("form.queries")>
	 <cfinclude template="testme2.cfm">
</cfif>     





 <!--- 
 
 THIS IS THE ORIGINAL CODE
 
 <!-- Form makes an HTTP GET request to mqlread, results go in iframe r -->
<!---   <form action="http://www.freebase.com/api/service/mqlread"
        method="get"
        target="r"> --->
		
		  <form action="http://www.cf8ourexplorer.com:8500/oe/testme_results.cfm"
        method="get"
        target="r">
  <!-- Force mqlread to return text/javascript instead of application/json -->
  <input type="hidden" name="callback" value=" ">
  <!-- An HTML table to display the query, buttons, and result -->

  <table> 
    <tr><th>Query</th><td></td><th>Result</th></tr>    <!-- table header -->
    <tr>

      <td class="border">
        <!-- enter query -->
        <textarea name="queries" id="q">
{
  "qname": {
    "query": [{
      
  "type" : "/music/artist",
  "name" : "The Police",
  "album" : []

    }]
  }
}
        </textarea></td> 
      <td valign="top"> 
        <input type="submit" value="Send"><br>         <!-- send query -->

        <input type="reset" value="Erase"></td>        <!-- erase query  -->
      <td class="border">
        <iframe name="r" id="r"></iframe></td>         <!-- results go here -->
    </tr>

  </table>   ---> 


</body>
</html>
</body></html>
-------------- next part --------------
<cffunction
	name="GetResponseCookies"
	access="public"
	returntype="struct"
	output="false"
	hint="This parses the response of a CFHttp call and puts the cookies into a struct.">
 
	<!--- Define arguments. --->
	<cfargument
		name="Response"
		type="struct"
		required="true"
		hint="The response of a CFHttp call."
		/>
 
 
	<!--- Define the local scope. --->
	<cfset var LOCAL = StructNew() />
 
	<!---
		Create the default struct in which we will hold
		the response cookies. This struct will contain structs
		and will be keyed on the name of the cookie to be set.
	--->
	<cfset LOCAL.Cookies = StructNew() />
 
	<!---
		Get a reference to the cookies that werew returned
		from the page request. This will give us an numericly
		indexed struct of cookie strings (which we will have
		to parse out for values). BUT, check to make sure
		that cookies were even sent in the response. If they
		were not, then there is not work to be done.
	--->
	<cfif NOT StructKeyExists(
		ARGUMENTS.Response.ResponseHeader,
		"Set-Cookie"
		)>
 
		<!---
			No cookies were send back in the response. Just
			return the empty cookies structure.
		--->
		<cfreturn LOCAL.Cookies />
 
	</cfif>
 
 
	<!---
		ASSERT: We know that cookie were returned in the page
		response and that they are available at the key,
		"Set-Cookie" of the reponse header.
	--->
 
 
	<!---
		Now that we know that the cookies were returned, get
		a reference to the struct as described above.
	--->
	<cfset LOCAL.ReturnedCookies = ARGUMENTS.Response.ResponseHeader[ "Set-Cookie" ] />
 
 
	<!--- Loop over the returned cookies struct. --->
	<cfloop
		item="LOCAL.CookieIndex"
		collection="#LOCAL.ReturnedCookies#">
 
 
		<!---
			As we loop through the cookie struct, get
			the cookie string we want to parse.
		--->
		<cfset LOCAL.CookieString = LOCAL.ReturnedCookies[ LOCAL.CookieIndex ] />
 
 
		<!---
			For each of these cookie strings, we are going to
			need to parse out the values. We can treate the
			cookie string as a semi-colon delimited list.
		--->
		<cfloop
			index="LOCAL.Index"
			from="1"
			to="#ListLen( LOCAL.CookieString, ';' )#"
			step="1">
 
			<!--- Get the name-value pair. --->
			<cfset LOCAL.Pair = ListGetAt(
				LOCAL.CookieString,
				LOCAL.Index,
				";"
				) />
 
 
			<!---
				Get the name as the first part of the pair
				sepparated by the equals sign.
			--->
			<cfset LOCAL.Name = ListFirst( LOCAL.Pair, "=" ) />
 
			<!---
				Check to see if we have a value part. Not all
				cookies are going to send values of length,
				which can throw off ColdFusion.
			--->
			<cfif (ListLen( LOCAL.Pair, "=" ) GT 1)>
 
				<!--- Grab the rest of the list. --->
				<cfset LOCAL.Value = ListRest( LOCAL.Pair, "=" ) />
 
			<cfelse>
 
				<!---
					Since ColdFusion did not find more than one
					value in the list, just get the empty string
					as the value.
				--->
				<cfset LOCAL.Value = "" />
 
			</cfif>
 
 
			<!---
				Now that we have the name-value data values,
				we have to store them in the struct. If we are
				looking at the first part of the cookie string,
				this is going to be the name of the cookie and
				it's struct index.
			--->
			<cfif (LOCAL.Index EQ 1)>
 
				<!---
					Create a new struct with this cookie's name
					as the key in the return cookie struct.
				--->
				<cfset LOCAL.Cookies[ LOCAL.Name ] = StructNew() />
 
				<!---
					Now that we have the struct in place, lets
					get a reference to it so that we can refer
					to it in subseqent loops.
				--->
				<cfset LOCAL.Cookie = LOCAL.Cookies[ LOCAL.Name ] />
 
 
				<!--- Store the value of this cookie. --->
				<cfset LOCAL.Cookie.Value = LOCAL.Value />
 
 
				<!---
					Now, this cookie might have more than just
					the first name-value pair. Let's create an
					additional attributes struct to hold those
					values.
				--->
				<cfset LOCAL.Cookie.Attributes = StructNew() />
 
			<cfelse>
 
				<!---
					For all subseqent calls, just store the
					name-value pair into the established
					cookie's attributes strcut.
				--->
				<cfset LOCAL.Cookie.Attributes[ LOCAL.Name ] = LOCAL.Value />
 
			</cfif>
 
		</cfloop>
 
 
	</cfloop>
 
 
	<!--- Return the cookies. --->
	<cfreturn LOCAL.Cookies />
</cffunction>

<!---
	Get the URL of the application page that we want to
	grab. This page lives under a different application
	and will have different session management (that we
	must account for in subsequent requests).
--->
<cfset strURL = ("http://www.freebase.com/api/account/login") />
 
<!---
	Store the user agent that we want to send (so CFHttp
	doesn't send "ColdFusion" as the user agent). Since I
	am testing this in a browser, I am just going to grab
	the current user agent.
--->
<cfset strUserAgent = CGI.http_user_agent />
 
 
<!---
	Grab the first page request. In this one, we are not
	going to sent any CFHttpParams since we don't know
	anything about the target application environment.
--->
<cfhttp
	method="POST"
	url="#strURL#"
	result="objGet" 
	>
	<cfhttpparam name="username" type="FORMFIELD" value="markce">
	<cfhttpparam name="password" type="FORMFIELD" value="MYPASSWORD">
	</cfhttp>
 
<!--- Dump out the CFHttp response. --->
<!--- <cfdump
	var="#objGet#"
	label="First CFHttp Request"
	/>  --->
 
 
<!---
	Get the cookies from the last response object. These are
	the cookies that we are going to echo back in subsequent
	CFHttp requests.
--->
<cfset objCookies = GetResponseCookies( objGet ) />

<cfdump var="#objcookies#" label="Cookies">
<cfdump var="#form#" label="Form">

<!---
	Now, let's make a subsequent CFHttp call, but this
	time, we are going to pass in the cookies that were
	returned in the last call to see if we can maintain
	the same session.
--->
<cfset strURL = ("http://www.freebase.com/api/service/mqlread") />
<cfhttp url="#strURL#" method="POST" result="objGet">
	<cfhttpparam name="q" value="#form.queries#" type="FORMFIELD">
	<cfhttpparam name="metaweb-user"	 value="#objcookies["metaweb-user"].value#" type="cookie">
</cfhttp>
	
	
 
 
<!--- Dump out the CFHttp response. --->
<cfdump
	var="#(objGet.responseheader)#"
	label="mqlread response Header"
	/>	
<cfdump
	var="#deSerializeJSON(objGet.filecontent)#"
	label="mqlread response Detail"
	/>		
-------------- next part --------------


No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.476 / Virus Database: 269.11.6/938 - Release Date: 05/08/2007 16:16


More information about the Developers mailing list