3

This is seriously driving me nuts!!

The exact same request succeeds when I format it as a curl statement, but gets a 404 when using cfhttp to make the request.

Coldfusion

<cfhttp method="POST" result="stepsResponse"
    url="https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate">
    <cfhttpparam type="header" name="Authorization" value="Bearer #ACCESS_TOKEN#">
    <cfhttpparam type="header" name="Content-Type" value="application/json;encoding=utf-8">
    <cfhttpparam type="body" value='{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}'>
</cfhttp>

Curl

curl \
-X POST \
-H "Content-Type: application/json;encoding=utf-8" \
-H "Authorization: Bearer {ACCESS_TOKEN}" \
-d '{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}' \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

Why? Why? Why?

Strangely, when I call the endpoint directly in the browser I get a 404 as well. I'm fairly certain there is something (easy) that I'm overlooking, but I've been staring at this for hours over the past couple days and cannot see where I'm going wrong. I sincerely hope someone out there sees the problem. I'm grateful for your time!

Google Developer Guide — Read Daily Steps Total

Update: JavaScript Works TOO! Why won't CFHTTP?!

2023-03-18: I've tested this using JavaScript's fetch method as well and it works. No 404. I just don't understand why the same URL fails in CFHTTP?!

fetch('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', {
  method: 'POST',
  headers: {
    'Authorization': '#ACCESS_TOKEN#',
    'Content-Type': 'application/json;encoding=utf-8'
  },
  body: '{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}'
})
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

Update: stepsResponse Screenshot

2023-03-22: at the request of @CFMLBread and @TRose, here's a screenshot of the stepsResponse.

enter image description here

Chris Geirman
  • 9,474
  • 5
  • 37
  • 70
  • Well loading it in your browser counts as a GET request. So that'll never work if it's expecting a POST. If you dump the ColdFusion variable `stepsResponse` can you give us a more detailed error message? Maybe wrap the whole thing in `` and `` and dump that? – TRose Mar 17 '23 at 04:02
  • @TRose — I know it expects POST, but I hoped a GET request would at least return something other than a 404. Anything but a 404. A dump of the `stepsResponse` returns `status_code: 404` and the `filecontent` returns the html code for the 404 page. Same html as if you tried to [open the endpoint](https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate) as a GET request – Chris Geirman Mar 17 '23 at 17:31
  • Dump the result of the serializeJSON function. Make sure the result is what you expect it to be. Is there a reason you're using the function in the first place? – EddieLotter Mar 18 '23 at 01:01
  • @EddieLotter — thanks for your attention and great catch. As shown above, I wouldn't actually need the serializeJSON() function, but I actually create the object up higher in the code and have to serialize it when I output. I pasted the serialized output instead of the variable to make the example more simple. I've updated the code above to be more accurate. Thanks again! The problem truly is just that cfhttp sees the url as a 404 for some inexplicable reason – Chris Geirman Mar 18 '23 at 18:35
  • Can you post the dump of the return from google? #stepsResponse# – CFMLBread Mar 22 '23 at 20:42
  • @CFMLBread — Thank you for your attention to this. I've attached the requested screenshot of the stepsResponse dump. – Chris Geirman Mar 23 '23 at 03:47
  • Are you running your tests from the same server that ColdFusion is running on? – Miguel-F Mar 28 '23 at 18:38
  • @Miguel-F - the javascript fetch is happening from the same page as the coldfusion code. I have the page outputting the curl statement on the same page too, but I copy/paste it into the terminal to run. All are happening on the same computer with the same network access. – Chris Geirman Mar 28 '23 at 22:46

2 Answers2

1

I was having this exact same issue with cfhttp today. Turns out the colon in the url part was automatically being encoded, which the google api didn't understand. If you're on Lucee, you can use the 'encodeurl' option - https://docs.lucee.org/reference/tags/http.html . If you're on some other version of coldfusion you could try updating or going around cfhttp entirely and using something like bolthttp https://github.com/foundeo/bolthttp

croot
  • 26
  • 2
0

From an old Reddit post:

Try adding an "accept" header

Will Belden
  • 630
  • 4
  • 11
  • 1
    — Thanks for the suggestion, but it had no impact. I tried adding it both in the curl and in the cfhttp statements. Again, curl works but cfhttp returns 404. – Chris Geirman Mar 17 '23 at 17:26
  • Have you tried running the CFHTTP from a different CF server? I'm wondering if Google is blocking blocks of IPs and your CF server may be part of that? Grasping at straws, I know.... – CFMLBread May 17 '23 at 20:50