User Guide Cancel

http

 

Note:

In ColdFusion (2018 release), script functions implemented as CFCs are deprecated in ColdFusion.

Description

Used in CFScript to generate an HTTP request and handle the response from the server.

Syntax

Mode

Syntax

Creating the service

new http() or createObject("component","http")

Initializing the attributes

Any one of the following:

  • httpService=new http(_attribute-value_pair_)
  • httpService.setAttributes(_attribute-value_pair_)
  • httpService.set_AttributeName_(attribute_value)
  • httpService.send(attribute-value_pair)

Executing the service action

httpService.send(_attribute-value_pair_)

Properties

  • url
  • columns
  • getasbinary
  • name
  • proxyserver
  • redirect
  • throwonerror
  • charset
  • delimiter
  • method
  • password
  • proxyport
  • resolveurl
  • timeout
  • clientcert
  • file
  • multipart
  • path
  • proxyuser
  • result
  • useragent
  • clientcertpassword
  • firstrowasheaders
  • multiparttype
  • port
  • proxypassword
  • textqualifier
  • username

All attributes supported by the tag cfhttp can be used as attribute-value pairs. For example,

<cfhttp name="onerow">

can be used as

httpService.setName("onerow");

For details of the attributes, see the Attributes section for the tag cfhttp.

See also

History

ColdFusion 9: Added this function.

Methods

  • addParam

    Description

    Used to add  cfhttpparam  tags. For example, to specify  http  POST operations in CFScript. Specifies parameters to build an HTTP request.

    Syntax

    httpService.addParam(attribute-value pair)

    Returns

    Nothing

    Arguments

    All attributes supported by cfhttpparam tag can be used as attribute-value pairs.

  • send

    Description

    Used to generate an HTTP request and handle the response from the server.

    Returns

    A component on which the following methods can be invoked:

  • GetResult(): To access the query object returned by the server if a name attribute is specified.
  • GetPrefix(): To access the  cfhttp  scope. This is equivalent to the result attribute of the  cfhttptag .

    Syntax

    httpService.send(attribute_value pair)

    Arguments

    All attributes supported by the cfhttpparam tag.

  • setAttributes

    Description

    Sets attributes for the  http  function.

    Returns

    Nothing

    Syntax

    httpService.setAttributes (attribute-value pair)

    Arguments

    All arguments supported by the cfhttp tag.

  • getAttributes

    Description

    Gets attributes that were set for the  http  function.

    Returns

    Returns  a struct with all or some of the service tag attribute values.

    Syntax

    httpService.get_Attributes_(attribute_list)

    Arguments

    A comma-separated list of attributes. If no list is specified, all defined attributes are returned.

  • clearAttributes

    Description

    Removes all attributes added for the  http  function.

    Returns

    Nothing

    Syntax

    httpService.clearAttributes(attribute_list)

    Arguments

    A comma-separated list of attributes.

  • clearParams

    Description

    Removes  cfhttpparam  tags that were added using the addParam method.

    Returns

    Nothing

    Syntax

    httpService.clearParams()

    Arguments

    None

  • clear

    Description

    Removes all attributes and cfhttpparam tags that were added using the addParam method.

    Returns

    Nothing

    Syntax

    httpService.clear()

    Arguments

    None

Usage

This function corresponds to the cfhttp tag. For usage details, see the Usage section for cfhttp in the CFML Reference.

Example

<!---
<cfset videoName = "<video path>\hello.wmv">
<cfset videoFileName = "hello.wmv">
--->
<!--- Set User Account Data --->
<!---
<cfset clientKey = "enter client key from google"/>
<cfset devKey = "ebtdev key from google>"/>
--->
<cfscript>
/* youtube uplaod url */
youTubeUploadURL = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
/* video to upload */
videoName = ExpandPath('./hello.wmv');
videoFileName = "hello.wmv";
/* set user account data */
clientKey = "enter client key from google";
devKey = "ewnter dev key from google";
/* create new http service */
httpService = new http();
/* set attributes using implicit setters */
httpService.setMethod("post");
httpService.setCharset("utf-8");
httpService.setUrl("https://www.google.com/accounts/ClientLogin");
/* add httpparams using addParam() */
httpService.addParam(type="formfield",name="accountType",value="HOSTED_OR
_GOOGLE");
httpService.addParam(type="formfield",name="Email",value="enter gmail id");
httpService.addParam(type="formfield",name="Passwd",value="enter password");
httpService.addParam(type="formfield",name="service",value="youtube");
httpService.addParam(type="formfield",name="source",value="youtubecode");
/* make the http call to the URL using send() */
result = httpService.send().getPrefix();
/* process the filecontent returned */
content = listtoarray(result.filecontent,chr(10));
for(i=1;i lte arraylen(content);i++)
{
item = content[i];
authdata[listFirst(item, "=")] = listRest(item, "=");
}
</cfscript>
<!--- Create ATOM XML and save to a file to be sent with video --->
<cfsavecontent variable="meta">
<cfoutput>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">WithOutQuotes</media:title>
<media:description type="plain">Test Description</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>yourvideo</media:keywords>
</media:group>
</entry>
</cfoutput>
</cfsavecontent>
<cfscript>
tmpfile = expandPath("./meta.xml");
FileWrite(tmpfile,trim(meta));
/* use the httpService created above */
httpService.setUrl("http://uploads.gdata.youtube.com/feeds/api/users/default/
uploads");
httpService.setTimeOut(450);
httpService.setMultipartType("related");
/* clear params first */
httpService.clearParams();
/* add httpparams using addParam() */
httpService.addParam(type="header",name="Authorization", value="GoogleLogin auth=#authdata.auth#");
httpService.addParam(type="header",name="X-GData-Client", value="#variables.clientkey#");
httpService.addParam(type="header",name="X-GData-Key", value="key=#variables.devkey#");
httpService.addParam(type="header",name="Slug",value="#videoFileName#");
httpService.addParam(type="file",name="API_XML_Request",file="#tmpfile#",
mimetype=
"application/atom+xml");
httpService.addParam(type="file",name="file",file="#videoName#",mimetype
="video/*");
/* make the http call to the URL using send() */
result = httpService.send().getPrefix();
if(result.statuscode contains "201")
{
WriteOutput("Your video has been successfully uploaded to YouTube");
}
else
{
WriteOutput("There was a problem uploading the video. Status code returned was " & result.statuscode);
}
</cfscript>
<!--- <cfset videoName = "<video path>\hello.wmv"> <cfset videoFileName = "hello.wmv"> ---> <!--- Set User Account Data ---> <!--- <cfset clientKey = "enter client key from google"/> <cfset devKey = "ebtdev key from google>"/> ---> <cfscript> /* youtube uplaod url */ youTubeUploadURL = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"; /* video to upload */ videoName = ExpandPath('./hello.wmv'); videoFileName = "hello.wmv"; /* set user account data */ clientKey = "enter client key from google"; devKey = "ewnter dev key from google"; /* create new http service */ httpService = new http(); /* set attributes using implicit setters */ httpService.setMethod("post"); httpService.setCharset("utf-8"); httpService.setUrl("https://www.google.com/accounts/ClientLogin"); /* add httpparams using addParam() */ httpService.addParam(type="formfield",name="accountType",value="HOSTED_OR _GOOGLE"); httpService.addParam(type="formfield",name="Email",value="enter gmail id"); httpService.addParam(type="formfield",name="Passwd",value="enter password"); httpService.addParam(type="formfield",name="service",value="youtube"); httpService.addParam(type="formfield",name="source",value="youtubecode"); /* make the http call to the URL using send() */ result = httpService.send().getPrefix(); /* process the filecontent returned */ content = listtoarray(result.filecontent,chr(10)); for(i=1;i lte arraylen(content);i++) { item = content[i]; authdata[listFirst(item, "=")] = listRest(item, "="); } </cfscript> <!--- Create ATOM XML and save to a file to be sent with video ---> <cfsavecontent variable="meta"> <cfoutput> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">WithOutQuotes</media:title> <media:description type="plain">Test Description</media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People </media:category> <media:keywords>yourvideo</media:keywords> </media:group> </entry> </cfoutput> </cfsavecontent> <cfscript> tmpfile = expandPath("./meta.xml"); FileWrite(tmpfile,trim(meta)); /* use the httpService created above */ httpService.setUrl("http://uploads.gdata.youtube.com/feeds/api/users/default/ uploads"); httpService.setTimeOut(450); httpService.setMultipartType("related"); /* clear params first */ httpService.clearParams(); /* add httpparams using addParam() */ httpService.addParam(type="header",name="Authorization", value="GoogleLogin auth=#authdata.auth#"); httpService.addParam(type="header",name="X-GData-Client", value="#variables.clientkey#"); httpService.addParam(type="header",name="X-GData-Key", value="key=#variables.devkey#"); httpService.addParam(type="header",name="Slug",value="#videoFileName#"); httpService.addParam(type="file",name="API_XML_Request",file="#tmpfile#", mimetype= "application/atom+xml"); httpService.addParam(type="file",name="file",file="#videoName#",mimetype ="video/*"); /* make the http call to the URL using send() */ result = httpService.send().getPrefix(); if(result.statuscode contains "201") { WriteOutput("Your video has been successfully uploaded to YouTube"); } else { WriteOutput("There was a problem uploading the video. Status code returned was " & result.statuscode); } </cfscript>
<!---
<cfset videoName = "<video path>\hello.wmv">
<cfset videoFileName = "hello.wmv">
--->
<!--- Set User Account Data --->
<!---
<cfset clientKey = "enter client key from google"/>
<cfset devKey = "ebtdev key from google>"/>
--->
<cfscript>
/* youtube uplaod url */
youTubeUploadURL = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
/* video to upload */
videoName = ExpandPath('./hello.wmv');
videoFileName = "hello.wmv";
/* set user account data */
clientKey = "enter client key from google";
devKey = "ewnter dev key from google";
/* create new http service */
httpService = new http();
/* set attributes using implicit setters */
httpService.setMethod("post");
httpService.setCharset("utf-8");
httpService.setUrl("https://www.google.com/accounts/ClientLogin");
/* add httpparams using addParam() */
httpService.addParam(type="formfield",name="accountType",value="HOSTED_OR
_GOOGLE");
httpService.addParam(type="formfield",name="Email",value="enter gmail id");
httpService.addParam(type="formfield",name="Passwd",value="enter password");
httpService.addParam(type="formfield",name="service",value="youtube");
httpService.addParam(type="formfield",name="source",value="youtubecode");
/* make the http call to the URL using send() */
result = httpService.send().getPrefix();
/* process the filecontent returned */
content = listtoarray(result.filecontent,chr(10));
for(i=1;i lte arraylen(content);i++)
{
item = content[i];
authdata[listFirst(item, "=")] = listRest(item, "=");
}
</cfscript>
<!--- Create ATOM XML and save to a file to be sent with video --->
<cfsavecontent variable="meta">
<cfoutput>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">WithOutQuotes</media:title>
<media:description type="plain">Test Description</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>yourvideo</media:keywords>
</media:group>
</entry>
</cfoutput>
</cfsavecontent>
<cfscript>
tmpfile = expandPath("./meta.xml");
FileWrite(tmpfile,trim(meta));
/* use the httpService created above */
httpService.setUrl("http://uploads.gdata.youtube.com/feeds/api/users/default/
uploads");
httpService.setTimeOut(450);
httpService.setMultipartType("related");
/* clear params first */
httpService.clearParams();
/* add httpparams using addParam() */
httpService.addParam(type="header",name="Authorization", value="GoogleLogin auth=#authdata.auth#");
httpService.addParam(type="header",name="X-GData-Client", value="#variables.clientkey#");
httpService.addParam(type="header",name="X-GData-Key", value="key=#variables.devkey#");
httpService.addParam(type="header",name="Slug",value="#videoFileName#");
httpService.addParam(type="file",name="API_XML_Request",file="#tmpfile#",
mimetype=
"application/atom+xml");
httpService.addParam(type="file",name="file",file="#videoName#",mimetype
="video/*");
/* make the http call to the URL using send() */
result = httpService.send().getPrefix();
if(result.statuscode contains "201")
{
WriteOutput("Your video has been successfully uploaded to YouTube");
}
else
{
WriteOutput("There was a problem uploading the video. Status code returned was " & result.statuscode);
}
</cfscript>

Get help faster and easier

New user?