cfhttp and setting the content-type for a request

New Here ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

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

TOPICS
Server administration

Views

17.4K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

jimcpl,

If you read his first post, that is exactly what he is trying to do.

Jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

Hi Jason,

I re-read the thread, and sorry, I missed that ...  Got it now ...

Jim

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

I do not have any contact with the website owner so i cannot contact them. I realise setting the content-type in this manor is useless but the website requires it for some reason.Would still love to know a way to get around this problem. Futhermore it is also strange that the cfhttp tag strips a header that i instruct it to use.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

Just for fun, try this:

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

@12Robots: Brilliant it works not sure why but it works. Thank you very much this solves a really anoying problem.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

It works because CGI.CONTENT_TYPE is equivilent to the header Content-Type.  I found something about it in the docs for the Java HTTTPServletRequest. So I thought it was worth a try.

jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

Damn replied to soon it still doesn't work i it does send Content_Type: application/json; charset=utf-8 but apparently the website requires Content-Type: application/json; charset=utf-8. So i am still stucked.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

Then I'm afraid you're hosed.

You could try dropping down into Java and usign that, or using cURL from the command line or somehting. But I doubt there is anyway to force CF to make that invalid HTTP call.

I hope you are not doing something business critical with a service where you can't even get a hold of their admins to tell them something like this is wrong. Frankly. I would question the wisdom of depending on such a service.

GET requests do not have Content-Type headers. Period. There is no need for them, and the fact that they require it is stupid.

Can you say what service this is? 

Jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

12Robots - are you sure about your statement that GET method requests cannot have Content-Type headers?  I don't think that that is correct.  I've always thought that it was valid (maybe unusual, but ...) and this W3C link weems to agree, especially in the context of its comment about what Content-Type means in a HEAD method request: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

The big problem with trying to get around the CFHTTP limitation by using the CGI scope is that the dash is illegal in a CF varname.

There is a custom tag called CFHTTP5 that getsaround a lot of the CFHTTP limitations, including cookie preservation across multiple requests.  I haven't used it for a long time (CF5), but it is still actively developed.  Costs $50 - http://www.cftagstore.com/tags/cfxhttp5.cfm

-reed

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

Yes, I am sure.

If you are referring to this line in the RFC:

The Content-Type entity-header field indicates the media type of the

   entity-body sent to the recipient or, in the case of the HEAD method,

   the media type that would have been sent had the request been a GET.

That is referring to the RESPONSE header "Content-Type" not the REQUEST header. So if a GET request was made for a GIF image, then the response would include a Content-Type: image/gif header. Which is describing the body.

Content-Type is for describing the body of a request or response. If a request/response doesn't have a body, it doesn't need a Content-Type header. The exception to that is for the response to a  HEAD request where the response will include the Content-Type to indicated the type of the body that would have been returned. Since a HEAD request is a request only for the response headers, this makes perfect sense.

jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

So it looks like cURL will let you do it.

For example:

curl --header "Content-Type: text/html" http://www.google.com

http://screencast.com/t/UWV0LSXQs

Maybe you could do it that way in combination with <cfexecute>

Jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

12Robots wrote:

Just for fun, try this:

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

@Nebu23, @12Robots

Am I seeing things, or did'nt Nebu23 mark this as the answer? It helps to mark answered questions as answered.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

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

Jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 16, 2012 Mar 16, 2012

Copy link to clipboard

Copied

12Robots wrote:

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

Jason

Ah, thanks for clearing that up.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2012 Mar 15, 2012

Copy link to clipboard

Copied

Then you may be hosed. Cause I doubt you are going to be able to do it.

I suggest contacting the administrators of that service and find out why they require a useless header. 

Are you even passing along any JSON data?  Why would you need to tell them that your bodyless post contains JSON data, when it doesn't? 

Jason

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 17, 2012 Mar 17, 2012

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 18, 2012 Mar 18, 2012

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2012 Mar 20, 2012

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

LATEST

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 !

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines