Redirect client to a URL from a JS API service or from an html call

I am writing a service in nodeJS as an API service running on your servers.

The service invokes a call to an external API and receives an url.
Next step I need is to redirect the calling client (webbrowser using your client SDK) to that url.,

In the docs I don’t find a solution to redirect my web browser to another (in my case external) web page.

I would expect something like i.e. Backendless.redirect(303, url)
or the ability to push a route or so.

But since this maybe can’t work with calls from client SDK, I am thinking about another solution as well. The client may set up a HTML post request (via html form tag) to a certain URL in the web section of backendless. And there, a node.js script shall be invoked that calls the external API (in my case paypal or stripe) and then sends an html redirect back to the client. So the client would switch to stripes or PayPals checkout page (Thats how they do it in the current API). Is that possible?

Hello @Odo_Maletzki

I’ve created an example of service.
In response to this service, I set headers and status code

class MyService {
  makeRedirect() {
    this.response.statusCode = 303
    this.response.headers.Location = 'https://example.com'
 }
}

Backendless.ServerCode.addService( MyService );

Regards
Viktor

Ok, Thank you very much.

In my case it turned out to be a little more complex because I am calling an external API and then I need to redirect, when the external API call returns a result.

I did not use the promise correctly at the beginning. Thats why I failed.
So here is, how it woks for me to call an external API async, wait for the result, and then redirect the client.

MyAsyncExternalApiCall() {
  return new Promise((resolve, reject) => {
    CallToExternalAPI()
    .then((result) => { 
      this.response.statusCode = 303;
      this.response.headers.Location = result.redirectionURL;
      resolve();
    })
    .catch((result) => {
      console.log(""error. promise rejected");
      reject();
    });
  });
}

So this issue is closed for me.
Thanks again

Hi @Odo_Maletzki

Since, the BL supports async/await let me show a little compact variation of your solution

async MyAsyncExternalApiCall() {
  const result = await CallToExternalAPI()

  this.response.statusCode = 303;
  this.response.headers.Location = result.redirectionURL; 
}

Regards, Vlad

Hi @vladimir-upirov

Yes, your code is compact and nice.

Thank you very much.