HISE adds trailing slash to server post calls
-
HISE adds a trailing slash to the end of every route for post calls.
So:
POST www.example.com/test
will become
POST www.example.com/test/
I think its not correct to add a trailing slash automatically because its not the same route in the end. I am working on a project with supabase which automatically generates the routes based on the table schema which does not add a trailing slash to every route.
This is the code in the
ScriptingApi
callWithPost function.const bool isNotAFile = !subURL.containsChar('.'); const bool trailingSlashMissing = !subURL.endsWithChar('/'); if(isNotAFile && trailingSlashMissing) { // We need to append a slash in order to prevent redirecting to a GET call subURL << '/'; }
I've removed that code and its working in HISE I can use the correct routes without the added /. However when exporting my app to a standalone or a vst I get no network calls in my app.
What do you think should POST call automatically add a trailing slash?
-
@oskarsh It's been a while since I've wrote this, so I might not remember the details (however the comment suggests there was a nasty bug when not appending slashes). IIRC it was something that didn't work on macOS - if you forget to add a trailing slash, the Windows server call still goes through, so that was my lousy attempt of making it consistent.
My knowledge of server / web APIs is very limited, so if you have a suggestion how to improve that, let me know.
-
@Christoph-Hart I cannot comment on the actual architecture since I am not proficient in C++ enough but it seems like you are using a simple wrapper around the JUCE code and a queue for the requests to pass thru.
There is also this line:
p->isPost = true;
so the request that is sent to the server contains the information. I think the problem is in this function [0]:
void ScriptingApi::Server::callWithPOST(String subURL, var parameters, var callback) { if (HiseJavascriptEngine::isJavascriptFunction(callback)) { GlobalServer::PendingCallback::Ptr p = new GlobalServer::PendingCallback(getScriptProcessor(), callback); p->url = getWithParameters(subURL, parameters); <- should be withPOSTData p->isPost = true; globalServer.addPendingCallback(p); } }
I think the getWithParameter call should be
withPOSTData
[1].I believe its save to remove the trailing slash code like I did. I can verify its working on Linux and Windows. I think the code needs a slight modification to use
withPOSTData
instead ofgetWithParameter
. Sadly I cannot verify this on MacOS since I do not have access to my macbook right now.[0] -> https://github.com/christophhart/HISE/blob/2d88ad47af71047dede7769b9794f0707aed83d4/hi_scripting/scripting/api/ScriptingApi.cpp#L6983
[1] -> https://docs.juce.com/master/classURL.html#a424c1066a6e5c978ca7bdfab2a757889#withPOSTData -
I just hit this problem too. My rest endpoint doesn't have the slash, but HISE adds it so my call doesn't work. I'm trying to tackle it from the other end by adding the slash to my endpoint.
-
@d-healey while adding a slash to the endpoint certainly works i still believe that it's very bad practice to alter the called url automatically. From my experience as a web developer many rest API s do not contain a slash at the end and it's not the same rest url in the end. However there is no official standard.
I would very much welcome if this issue gets a second look. I tested my modification on Linux and Windows and it works well but I had no chance of testing macos yet for any side effects.
https://stackoverflow.com/questions/61547014/restful-uri-trailing-slash-or-no-trailing-slash
-
@oskarsh alright, so what we leave the default as it is, but add a
Server.setUseTrailingSlash(true)
function that let's you disable that project-wise? -
@Christoph-Hart I like it
-
@d-healey Alright, I've added
Server.setEnforceTrailingSlash(bool shouldBeAdded)
so you can change this.It won't show up in the API yet, so it's a magic spell until I'll rebuild the API docs on a Windows system :)
-
@Christoph-Hart Thanks Christoph for jumping onto this so quickly. Its working well on my end!
-
small necro to report that there's a leading slash issue which can only be mitigated by setBaseURL