February 12, 2023
CFHTTP Call get method and passing data json
Comments
(2)
February 12, 2023
CFHTTP Call get method and passing data json
Newbie 1 posts
Followers: 0 people
(2)

Hi i am trying replicate this call by PHP to Coldfusion:

The call must be by method GET but passing data json in body {“parameter”: “2222222”}

In PHP works fine but in coldfsuion no.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://{—URL—});
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘GET’);
curl_setopt($ch, CURLOPT_POSTFIELDS, “{‘param’: ‘2222222’}”);
$headers = array();
$headers[] = ‘X-Api-Token: {—TOKEN–}’;
$headers[] = ‘Content-Type: application/json’;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Coldfusion not works fine:
<cfhttp  url=”http://—-URL—” method=”GET” result=”res” charset=”utf-8″>
    <cfhttpparam type=”header” name=”X-API-Token” value=”—TOKEN—” >
    <cfhttpparam type=”header” name=”Content-Type” value=”application/json” >
   <cfhttpparam type=”body” value=’#SerializeJSON({“param”:”22222222″})#’ >
</cfhttp>
<cfdump var=”#res.filecontent#”>
Can you help me, please?
2 Comments
2023-02-14 01:50:36
2023-02-14 01:50:36

Also, what is it that doesn’t work? Can you share any error message if not also the dump of your res variable?

Also, have you tried outputting your SerializeJSON({“param”:”22222222″}), to make sure that really looks like what you show sending in the php code?

Note also that your php code sets curl_setopt($ch, CURLOPT_POSTFIELDS, which according to the php docs is for sending a form POST, though I see also the use of CURLOPT_CUSTOMREQUEST, ‘GET’);.

Have you at least TRIED doing a method=”post” instead of get?

Finally, here’s something else to consider: is the php running from the same machine as cf? If not, try making the call using the free postman tool, first from your machine with php, then from the machine with CF. Your issue may not be with CF itself.

Or maybe someone else may have more on your specific goal.

Like
()
2023-02-14 01:21:00
2023-02-14 01:21:00

I seem to remember running into this issue years ago. Ah yes. This StackOverflow answer gives the gist of it:
https://stackoverflow.com/questions/978061/http-get-with-request-body#983458

Long story short:
“A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.”

Basically, a GET request is not expected to have a BODY, and the server doesn’t know how to handle it when it gets it.

If you can, check the target server and see what it’s receiving in the body. You may need to convert the body into URL parameters instead.

Like
()
Add Comment