POST Request in Velocity

216 views
Skip to first unread message

Jeff Crowder

unread,
Oct 29, 2021, 8:03:44 AM10/29/21
to dotCMS User Group
I'm attempting to make a POST request to an json API endpoint, but it seems that the request is not being made or at least that the result of the request is coming back as null. I've validated that the endpoint is working properly by making a request to it via postman.

Below is the code I'm running. (It's worth noting that this post request doesn't require any data in the body of the request)

#set($apiresult = $json.post($url, {}))

Any help to set me on the correct path would be appreciated!

Mark Pitely

unread,
Oct 29, 2021, 8:57:57 AM10/29/21
to dot...@googlegroups.com
Jeff,

I find $json.fetch($url) works better if you are not actually doing a POST. A POST is usually to send, not retrieve, data, or if you have to pass credentials to touch the API endpoint.
If you give us the endpoint (if it is not private) we can also test externally and give you working code. 


Mark Pitely
Marywood University

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/c11da734-93fc-4adf-aa2d-32770f775d67n%40googlegroups.com.

Jeff Crowder

unread,
Oct 29, 2021, 10:05:13 AM10/29/21
to dotCMS User Group
Mark,

Unfortunately, in this instance, the endpoint I'm hitting must be done via a POST even though no data is being passed through. I don't have the ability to modify that endpoint to allow it to accept get requests.

Falzone, Chris

unread,
Oct 29, 2021, 10:10:35 AM10/29/21
to dot...@googlegroups.com
Does the request body specifically need to be empty or could you add a dummy value in there?



--

Christopher Falzone

DevOps Engineer

A Q U E N T  /  VITAMIN T

Jeff Crowder

unread,
Oct 29, 2021, 10:12:10 AM10/29/21
to dotCMS User Group
Chris,

I've tried adding both dummy headers and dummy data in the request. Neither has made a difference.

Mark Pitely

unread,
Oct 29, 2021, 10:32:16 AM10/29/21
to dot...@googlegroups.com
Hrm. Perhaps share your full actual code, perhaps there is a typo or something we can spot? Does the log contain any hints, like a 500/400/403 error?
You could also route around the issue with a microservice/PHP call that does the POST for you?


Mark Pitely
Marywood University

Jeff Crowder

unread,
Oct 29, 2021, 10:42:32 AM10/29/21
to dotCMS User Group
Fortunately I was just now able to make progress on the issue.

It turns out, the endpoint was requiring a content length header to be sent along. Postman sends those by default, so I wasn't seeing the issue there. I tried making the call using a curl request and I got the notification that a content length was needed. Even though I was using the dotlogger, I wasn't receiving that notification through the velocity script. Is there someplace in dotcms logs that I would be able to see this information? Or do they bury it?

Mark Pitely

unread,
Oct 29, 2021, 11:15:21 AM10/29/21
to dot...@googlegroups.com
Great! 
What is logged or not logged is a mystery wrapped in a conundrum, or the more concise: YMMV.
I'm guessing there is something in the dotcms.log that would have pointed it out. Depends on your version, etc. 


M

Reply all
Reply to author
Forward
0 new messages