callWithPOST change content type
-
Re: Server.callWithPost content type
Hello everyone,
I'm currently working on implementing a licensing system using Moonbase within HISE. One issue I recently faced regarding user sign-in was working with the callWithPOST function. As the Moonbase API requires me to pass text/plain instead of a json object, I can't get the sign-in to properly work. Is there any way to just pass a simple string via the callWithPOST function? anyone else, who faced a similar issue and found an workaround?
Moonbase Docs Sign-In: https://moonbase.sh/docs/licensing/api/#sign-in-a-customer
inline function makeHttpRequest(method, endpoint, data, callback) { // Set base URL Server.setBaseURL(MOONBASE_API); Server.setHttpHeader("Content-Type: text/plain"); currentHttpCallback = callback; if (method == "POST") { Server.callWithPOST(endpoint, data, function(status, response) { if (status == Server.StatusOK) { Console.print("LicenseManager: Request successful"); currentHttpCallback(true, response); } else { Console.print("LicenseManager: Request failed with status: " + status); currentHttpCallback(false, {"error": "HTTP " + status}); } }); } else { Server.callWithGET(endpoint, data, function(status, response) { if (status == Server.StatusOK) { Console.print("LicenseManager: Request successful"); currentHttpCallback(true, response); } else { Console.print("LicenseManager: Request failed with status: " + status); currentHttpCallback(false, {"error": "HTTP " + status}); } }); } } /** * Sign in customer to get access token */ inline function signInCustomer(email, password, callback) { // Store callback in global variable for nested function access currentSignInCallback = callback; // Make sign-in request makeHttpRequest("POST", "/customer/identity/sign-in?email=" + email, password, function(success, response) { if (success) { Console.print("LicenseManager: Sign-in response received"); // Parse JSON response directly without storing in variable if (isDefined(parseSimpleJSON(response).accessToken)) { Console.print("LicenseManager: Access token received"); currentSignInCallback(true, parseSimpleJSON(response)); } else { Console.print("LicenseManager: No access token in response"); currentSignInCallback(false, "No access token received"); } } else { Console.print("LicenseManager: Sign-in request failed"); currentSignInCallback(false, "Sign-in request failed"); } }); }
@tobbentm any recommendation from your side on implementation? why cant we just pass json with the sign-in function? would have loved to use the JUCE module from you, but as you already pointed out, this is only compatible with JUCE 7.
Any help is greatly appreciated!
-
@Daanyoo looks OK and the
setHttpHeader
should change the content type, but your calls weirdly mix GET and POST parameters for the email and password so this might cause the issue.Can't you call
makeHttpRequest("POST", "/customer/identity/sign-in", { email: "my_email", password: "1234", }, // ...
This way both arguments are passed as POST parameters.
-
@Christoph-Hart ah actually there was a glitch in the URL handling which prevented this from work - passing in a string as POST data and then using the GET parameters is valid syntax but the JUCE URL class didn't parse the GET arguments correctly.
Try again now with your original snippet. Make sure that
password
is just the password as plain string, no JSON format required. -
@Christoph-Hart Okay great, this fix already allowed me to pass the plain string into the function.
Another problem, that also was mentioned in the referenced thread is that data is not being processed as text/plain or plain JSON as it looks like HISE is sending the data as URL-encoded form data (like a web form submission)
This leads to problems in calling the sign-in API endpoint of Moonbase - this is how Moonbase expects to receive sign-in data:
POST https://demo.moonbase.sh/api/customer/identity/sign-in?email=test@example.com Content-Type: text/plain Password1234!
I need to embed the mail into the URL endpoint and pass on the password as plain text. This is how I implemented it within HISE:
makeHttpRequest("POST", "/customer/identity/sign-in?email=" + email, password, function(success, response)
Due to the faulty URL encoding the data is being received in the format shown below:
I am not sure how to create a workaround for this..