Skip to main content
Participating Frequently
March 13, 2012
Answered

cfhttp and setting the content-type for a request

  • March 13, 2012
  • 4 replies
  • 21477 views

Hi,

I am trying to set the content-type for a cfhttp request like this (on coldfusion 9):

<cfhttp redirect="no" method="get" timeout="120" url="http://10.0.0.1/test2.cfm">

    <cfhttpparam type="HEADER" name="Content-Type" value="application/json; charset=utf-8">

</cfhttp>

// coding on test2.cfm:

<cfset x = GetHttpRequestData()>

<cfdump var=#x#>

// Output on test2.cfm

content [empty string]
headers
struct
Accept-Encoding deflate, gzip, x-gzip, compress, x-compress
Connection TE
Host 10.0.0.1:80
Proxy-Connection close
TE trailers, deflate, gzip, compress
User-Agent ColdFusion
X-REWRITE-URL http://10.0.0.1:80/test2.cfm
method GET
protocol HTTP/1.1

As you can see no content-type is send through. I also tried the sniffer.exe:

GET http://10.0.0.1:80/test2.cfm HTTP/1.1

Host: 127.0.0.1

Proxy-Connection: close

Connection: TE

TE: trailers, deflate, gzip, compress

User-Agent: ColdFusion

Accept-Encoding: deflate, gzip, x-gzip, compress, x-compress

Here you can also see that no content-type was passed through. The sniffer should report back

GET http://10.0.0.1:80/test2.cfm HTTP/1.1

Host: 127.0.0.1

Proxy-Connection: close

Connection: TE

TE: trailers, deflate, gzip, compress

User-Agent: ColdFusion

Accept-Encoding: deflate, gzip, x-gzip, compress, x-compress

Content-Type: application/json; charset=utf-8

But is does not, what do i need todo to set the content-type in a cfhttp request.

Kind regards,

Nebu

This topic has been closed for replies.
Correct answer gabrieleb5037809

Java is the way
I suppose that "under the hood" CFHTTP is cutting the "Content-Type" header for verbs other than PUT/POST.
This would be a wrong assumption (GET requests are not prohibited to have a "Content-Type" header even if they have -obviously- no body).
See this StackOverflow discussion on the subject.

Now the Java solution: the original code

<cfhttp redirect="no" method="get" timeout="120" url="http://10.0.0.1/test2.cfm">

    <cfhttpparam type="HEADER" name="Content-Type" value="application/json; charset=utf-8">

</cfhttp>

becomes (in CFScript)

<cfscript>

req = Createobject("java", "org.apache.http.client.fluent.Request");

res = req.get("http://10.0.0.1/test2.cfm ")
        .setHeader("Content-Type", "application/json; charset=utf-8")

       .execute()

        .returnContent();

</cfscript>

That's all, thank you Java !

4 replies

gabrieleb5037809Correct answer
Participant
January 9, 2019

Java is the way
I suppose that "under the hood" CFHTTP is cutting the "Content-Type" header for verbs other than PUT/POST.
This would be a wrong assumption (GET requests are not prohibited to have a "Content-Type" header even if they have -obviously- no body).
See this StackOverflow discussion on the subject.

Now the Java solution: the original code

<cfhttp redirect="no" method="get" timeout="120" url="http://10.0.0.1/test2.cfm">

    <cfhttpparam type="HEADER" name="Content-Type" value="application/json; charset=utf-8">

</cfhttp>

becomes (in CFScript)

<cfscript>

req = Createobject("java", "org.apache.http.client.fluent.Request");

res = req.get("http://10.0.0.1/test2.cfm ")
        .setHeader("Content-Type", "application/json; charset=utf-8")

       .execute()

        .returnContent();

</cfscript>

That's all, thank you Java !

Nebu23Author
Participating Frequently
March 18, 2012

He unmarked it as the answer, since it turned out it didn't work. Which is reasonable.

Correct i changed it to helpfull cause it could have worked but in my case it didn't. I am currenty writing a component based on java to bypass this issue in cfhttp. I will post this component here so anyone else encoutering this issue can also use it. I am aware of the cfx_http5 tag but i do not like to use anything that is not open source or part of coldfusion itself cause then i have to depent on others to solve issues.

Nebu23Author
Participating Frequently
March 20, 2012

Well posted my beta version on http://coldfusion9.blogspot.com/2012/03/custom-cfhttp-tag.html . This tag overcomes some of the problems with the cfhttp tag and response very similar to the standard cfhttp tag with some extra features.

BKBK
Community Expert
Community Expert
March 17, 2012

These seem to work, though they stand for something else

<cfhttpparam type="HEADER" name="Content_Type" value="application/json;charset=utf-8">

<cfhttpparam type="HEADER" name="'Content-Type'" value="application/json;charset=utf-8">

Hope this inspires someone.

12Robots
Participating Frequently
March 13, 2012

When used as a REQUEST header (which you are doing) the Content-Type header indicated the content type of the Entity Body of the request. This means it is only a valid header for requests that have a body (POST and PUT).  Change your CFHTTP type to POST and you will see the header show up.

jason

Nebu23Author
Participating Frequently
March 15, 2012

No idea why but i could not reply for some reason, anyway now i can again. First of all thanks for your response. I tried to change te request to a post which indeed does send the correct content-type. However the server which i am connecting to requires the following syntax:

GET {webpage} HTTP/1.1

Content-Type: application/json; charset=utf-8

Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)

Host: {domain}

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Changing Get to Post returns an error, so i need to be able to set the Content-Type for a Get request. Is this possible with cfhttp?

Participating Frequently
March 15, 2012

Hi,

I'm new to CF, but can't you add the Content-Type header using cfhttpparam:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_g-h_10.html

Jim