Cfhttp gzip post

if im using cfhttp to post data to and endpoint that supports gzip compressed payload. im setting the header to

Content-Encoding:gzip. 

how should i compress the content of the post ?

I would use mod_deflate if you are using apache and you want to reduce the size of the data in transit to to the supported end users browser.

apache doesn’t come into play as this is sending a http request not receiving one

there is a compression option, but i think it’s only used for request headers

I think what you are are after is something like a compress option for cfhttparam?

Yes exactly. I’m wanting to send the body content gzipped, to reduce network overhead. Where a server can receive it if it supports gzip compression. Specifically, I’m wanting to do this with an aws api gateway as I have read it supports it.

I had some time to revisit Gzipping post content to AWS api gateway. https://cflib.org/ had a udf that helped me out.

This is how you can do it.

<!---
 Compresses a string using the gzip algorithm; returns binary or a string of (base64|hex|uu).
 
 @param text      String to compress. (Required)
 @param format      binary,base64,hex, or uu. Defaults to binary. (Optional)
 @return Returns a string. 
 @author Oblio Leitch (oleitch@locustcreek.com) 
 @version 1, November 14, 2007 
--->
<cffunction name="gzip"
    returntype="any"
    displayname="gzip"
    hint="compresses a string using the gzip algorithm; returns binary or string(base64|hex|uu)"
    output="no">
    <!---
        Name	Description	Required
        text	String to compress.	Yes
        format	binary,base64,hex, or uu. Defaults to binary.	No
    --->
    <cfscript>
        var result="";
        var text=createObject("java","java.lang.String").init(arguments[1]);
        var dataStream=createObject("java","java.io.ByteArrayOutputStream").init();
        var compressDataStream=createObject("java","java.util.zip.GZIPOutputStream").init(dataStream);
        compressDataStream.write(text.getBytes());
        compressDataStream.finish();
        compressDataStream.close();

        if(arrayLen(arguments) gt 1){
            result=binaryEncode(dataStream.toByteArray(),arguments[2]);
        }else{
            result=dataStream.toByteArray();
        }
        return result;
    </cfscript>
</cffunction>

<cfset input = "my content to gzip">
<cfset compressed=gzip(input) />

<cfoutput>
original: #len(input)#<br />
compressed: #len(compressed)#<br />
encoded: #len(gzip(compressed,"base64"))#<br />
</cfoutput>

<cfset apipath="https://awsapiurl.amazonaws.com/">
<cfset xapikey="yourkeyhere">
<cfset contenttype="application/json">

<cfscript>
    cfhttp(method="POST", charset="utf-8", url="#trim(apipath)#", timeout="7", result="posteddata") {
        cfhttpparam(name="X-Api-Key", type="header", value="#xapikey#");         
        cfhttpparam(name="Content-Type", type="header", value="#contenttype#");
        cfhttpparam(name="Content-Encoding", type="header", value="gzip");
        cfhttpparam(type="body", value="#compressed#");
    }
</cfscript>

<CFDUMP var="#posteddata#">

@nalbee, Did you see this below function?

Thanks I did see that. As far as i can tell with compress you have to compress a file or directory. So you have you write a file 1st and read it. The code I posted you can compress what you already have in memory.

It would be great enhancement to compress. I’m not sure how to request that at lucee though.