Skip navigation
Currently Being Moderated

cfhttp and setting the content-type for a request

Mar 13, 2012 2:37 AM

Tags: #coldfusion9 #cfhttp

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

 
Replies
  • Currently Being Moderated
    Mar 13, 2012 7:56 AM   in reply to Nebu23

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 7:26 AM   in reply to Nebu23

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 8:01 AM   in reply to Nebu23

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 8:02 AM   in reply to jimcpl

    jimcpl,

     

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

     

    Jason

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 8:55 AM   in reply to 12Robots

    Hi Jason,

     

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

     

    Jim

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 10:05 AM   in reply to Nebu23

    Just for fun, try this:

     

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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 15, 2012 1:35 PM   in reply to Nebu23

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 10:19 AM   in reply to Nebu23

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 10:32 AM   in reply to 12Robots

    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

     

    
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 10:44 AM   in reply to 12Robots

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 11:02 AM   in reply to Reed Powell

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 11:03 AM   in reply to BKBK

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

     

    Jason

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 11:21 AM   in reply to 12Robots

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2012 11:21 AM   in reply to 12Robots

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2012 5:56 AM   in reply to Nebu23

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points