Cfhttpparam and encode

I am trying to send to the server a JSON string. If I use CFHTTP, cfhttp.filecontent becomes a Byte Array whereas when I use PostMan it returns a string as we expect.

What am I doing wrong?


var row = { a = 1, b = 2};

cfhttp( 
   url="http://127.0.0.1:8199/api/?event=user.create"
   method="post" 
   result="result" 
){
   cfhttpparam(type="body" encoded="false" value="#serializeJSON(row)#")
}

dump by cfhttp:

image

dump by postman
image

Don’t forget to tell us about your stack!

OS: Windows 10
Java Version: 11.0.11
Lucee Version: 5.3.7.48

what’s /api/?event=user.create doing when it returns ?

can you post the response headers from postman?

The api call returns a new user after inserting it in the database.

The problem mainly relies on the request body (before we even receive a response) which is encoded when sent by the cfhttp function.

To sum up, whereas when we call the api through postman the request body undergoes no change, when using cfhttp the fileContent gets converted into a byte array. We have found no solution in the documentation.

1 Like

I think this has to do with the content-type header returned by the server, try returning Content-Type: application/json from your API.

You should be able to get the text by doing toString(cfhttp.fileContent) if Content-Type doesn’t fix it.

Could try <cfhttp> :: Lucee Documentation getAsBinary=false

Thanks Pete!

If i add

cfhttpparam(type="header" name="Content-Type" value="application/json");

to my cfhttp, works correctly.

In your option is a correct behavior?

I encountered a similar issue in the past and now always add getasbinary=“no” to my CFHTTP requests. (NOTE: I’ve had to use “no” instead of boolean or else ACF will throw an error.)

1 Like